【问题标题】:Reading bitmap file returns null读取位图文件返回 null
【发布时间】:2020-03-16 11:47:20
【问题描述】:

我正在尝试将来自 URL 的文件保存为来自活动的位图文件,然后从其他一些活动中读取它并使用/显示它。

这是从 URL 下载文件的代码:

    private class AsyncTaskRunner extends AsyncTask<String, String, Bitmap> {

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    protected Bitmap doInBackground(String... params) {
            int userId = Integer.parseInt(params[0]);
            File file = new File(getFilesDir(), userId + ".png");
            Bitmap bitmapImage = null;
            switch ( params[1] ) {
                case "0":
                    try {
                        bitmapImage = BitmapFactory.decodeStream(new FileInputStream(file));
                        return bitmapImage;
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                        try {
                            file.createNewFile();
                            FileOutputStream out = new FileOutputStream(file);
                            String logoUri = "https://www.website.com/x/y/z/" + userId + ".png";
                            bitmapImage = getBitmapFromURL(logoUri);
                            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, out);
                            return bitmapImage;
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                break;

                case "1":
                    try {
                        bitmapImage = BitmapFactory.decodeStream(new FileInputStream(file));
                        return bitmapImage;
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                break;
            }
        return null;
    }


    @Override
    protected void onPostExecute(Bitmap result) {
        imageView.setImageBitmap(result);
    }

}

这是代码,我在其他活动中使用来读取下载的文件并显示它:

    private class AsyncTaskRunner extends AsyncTask<String, String, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... params) {
        int userId = Integer.parseInt(params[0]);
        File f = new File(getFilesDir(),userId + ".png");
        Bitmap bitmapImage = null;
        try {
            bitmapImage = BitmapFactory.decodeStream(new FileInputStream(f));
            return bitmapImage;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    protected void onPostExecute(Bitmap result) {
        // execution of result of Long time consuming operation
        imageView2.setImageBitmap(result);
    }

}

第一个 AsyncTask 类(带有 SWITCH 语句的类)似乎适用于下载和显示图像(例如:两种情况“0”和“1”)。

第二个 AsyncTask 没有... 如果我尝试,例如 System.out.print() 我得到的位图图像:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

还有一个奇怪的日志:

D/skia: --- Failed to create image decoder with message 'unimplemented'

另外,文件路径是:

/data/user/0/com.example.x.y/files/4.png

知道我应该怎么做吗?

【问题讨论】:

  • 嗯...文件存在吗?
  • 什么是位图文件?
  • '如果我尝试,例如对 System.out.print() 我得到的位图图像:'那么先显示你的代码。
  • 如果 f.exists() 返回 false,则不要调用第二个异步任务。

标签: java android android-studio bitmap android-asynctask


【解决方案1】:

您可以使用 next 读取原始文件。这非常适合我们的代码。

File mSaveBit; // Your image file
String filePath = mSaveBit.getPath()  
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);

【讨论】:

    【解决方案2】:

    嗨,谢谢你们的快速回复!

    对不起,我忘了说,是的,该文件存在。

    无论如何,我设法解决了这个问题,解决它的方法是在不使用 AsyncTask 类的情况下获取位图文件。

    代码如下:

                    File file = new File(getFilesDir(), userId + ".png");
                    Bitmap bitmapImage;
                    bitmapImage = BitmapFactory.decodeStream(new FileInputStream(file));
                    imageView.setImageBitmap(bitmapImage);
    

    【讨论】:

      猜你喜欢
      • 2013-05-07
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      • 2019-08-29
      • 2013-03-25
      相关资源
      最近更新 更多