我的应用程序无法在 Android Studio 上运行,我得到:E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException

问题描述 投票:0回答:1

这是我的代码:

package edu.huflit.comicsapp1;

public class App extends Application {
    public static ArrayList<Comics> data;

    @Override
    public void onCreate() {
        super.onCreate();
        if (data == null) {
            data = new ArrayList<>();
        }
    }

    public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            return BitmapFactory.decodeStream(input);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static Bitmap readBitmapFile(String path){
    return BitmapFactory.decodeFile(path);
}
    

    public static ArrayList<Comics> initDataForComics(){
        data.add(new Comics("SakamotoDays", readBitmapFile("https://i.pinimg.com/564x/57/2c/02/572c021d470fc2d22062d50a8df4a2cc.jpg")) );
        return data;

    }
}

如果我使用 readBitmapFile 函数运行此代码,我的应用程序将运行,但它不显示图像,并显示错误:

E/BitmapFactory:无法解码流:
java.io.FileNotFoundException:https:/i.pinimg.com/564x/57/2c/02/572c021d470fc2d22062d50a8df4a2cc.jpg:打开失败:ENOENT(没有这样的文件或目录)

如果我使用 getBitmapfromURL 函数运行此代码,我的应用程序将无法运行,我不知道为什么。

这是我的适配器:

package edu.huflit.comicsapp1;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class ComicsAdapter extends RecyclerView.Adapter<ComicsAdapter.ComicsVH> {


    ArrayList<Comics> lstComics;

    public ComicsAdapter(ArrayList<Comics> lstComics) {
        this.lstComics = lstComics;
    }

    @NonNull
    @Override
    public ComicsVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comics_rows,parent,false);
        return new ComicsVH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ComicsVH holder, int position) {
        Comics comics = lstComics.get(position);
        holder.imgComics.setImageBitmap(comics.getImage());
        holder.titleComics.setText(comics.getName());
    }

    @Override
    public int getItemCount() {
        return lstComics.size();
    }

    static class ComicsVH extends RecyclerView.ViewHolder{

        ImageView imgComics;
        TextView titleComics;
        public ComicsVH(@NonNull View itemView) {
            super(itemView);
            imgComics = itemView.findViewById(R.id.imgComics);
            titleComics = itemView.findViewById(R.id.titleComics);

        }
    }


}

这是我的主要活动:

package edu.huflit.comicsapp1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {


    RecyclerView rvComics;

    ComicsAdapter comicsAdapter;

    ArrayList<Comics> lstComics;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rvComics = findViewById(R.id.rvComics);

        lstComics = App.initDataForComics();

        comicsAdapter = new ComicsAdapter(lstComics);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this,
                                                                            LinearLayoutManager.VERTICAL,
                                                                            false);
        rvComics.setLayoutManager(linearLayoutManager);
        rvComics.addItemDecoration(new DividerItemDecoration(MainActivity.this,DividerItemDecoration.VERTICAL));
        rvComics.setAdapter(comicsAdapter);
    }


}

这是我的 Comic.java:

package edu.huflit.comicsapp1;

import android.graphics.Bitmap;
import android.net.Uri;

import org.w3c.dom.Text;

import java.lang.reflect.Array;
import java.net.URI;
import java.util.ArrayList;

public class Comics {

   int id;

   String name;
   int category;
   int views;
   int likes;
   Bitmap image;
    public Comics(String name, Bitmap image) {
        this.name = name;
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public Bitmap getImage() {
        return image;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void Image(Bitmap image) {
        this.image = image;
    }
}
android sqlite android-bitmap bitmapimage
1个回答
0
投票

You should call

getBitmapFromURL(String src)
function from another thread.

像这样改变你的漫画课:

    public class Comics {

    int id;

    String name;
    int category;
    int views;
    int likes;
    String imageUrl;
     public Comics(String name, String imageUrl) {
        this.name = name;
        this.imageUrl= imageUrl;
     }

     public String getName() {
        return name;
     }

     public String getImageUrl() {
        return imageUrl;
     }

     public void setName(String name) {
        this.name = name;
     }

     public void setImageUrl(String imageUrl) {
        this.imageUrl= imageUrl;
     }
 }

你的 onBindViewHolder 方法应该是这样的:

    @Override
    public void onBindViewHolder(@NonNull ComicsVH holder, int position) {
        Comics comics = lstComics.get(position);
        Runnable r = () -> {
           Bitmap bitmap = getBitMapFromUrl(comics.getImageUrl());
           new Handler(Looper.getMainLooper()).post(new Runnable() {
             @Override
             public void run() {
               holder.imgComics.setImageBitmap(bitmap);
             }
           });
        };
        Thread t = new Thread(r);

        t.start();
        
        holder.titleComics.setText(comics.getName());
    }

或者你可以使用 Glide 库,它更容易:

这是 gradle :

    repositories {
     google()
     mavenCentral()
   }

   dependencies {
     implementation 'com.github.bumptech.glide:glide:4.15.1'
   }

这是适配器功能:

@Override
    public void onBindViewHolder(@NonNull ComicsVH holder, int position) {
        Comics comics = lstComics.get(position);
        Glide.with(this).load(comics.getImageUrl()).into(holder.imgComics);
        
        holder.titleComics.setText(comics.getName());
    }
© www.soinside.com 2019 - 2024. All rights reserved.