简体   繁体   English

读取 bitmap 文件返回 null

[英]Reading bitmap file returns null

I'm trying to save a file from a URL as a bitmap file from an activity and then read it from some other activities and use / display it.我正在尝试将 URL 中的文件保存为活动中的 bitmap 文件,然后从其他一些活动中读取它并使用/显示它。

Here's the code to download the file from 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);
    }

}

And here is the code, I use on other activities to read the downloaded file and display it:这是代码,我在其他活动中使用来读取下载的文件并显示它:

    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);
    }

}

The first AsyncTask class ( the one with the SWITCH statement ) seems to work for both download & display the image ( eg: both cases "0" & "1" ).第一个 AsyncTask class (带有 SWITCH 语句的那个)似乎适用于下载和显示图像(例如:两种情况“0”和“1”)。

The second AsyncTask doesn't... If I try, for example to System.out.print() the bitmap image I get:第二个 AsyncTask 不...如果我尝试,例如 System.out.print() bitmap 图像,我得到:

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

And a weird log:还有一个奇怪的日志:

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

Also, the file path is:另外,文件路径是:

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

Any ideea what should I do?知道我该怎么做吗?

You can read raw file with next.您可以使用 next 读取原始文件。 Which is working perfect for our code.这非常适合我们的代码。

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

Hi & thank you for your fast replies guys!嗨,谢谢你们的快速回复!

Sorry, I forgot to mention, yes the file exists.抱歉,我忘了说,是的,该文件存在。

Anyway, I managed to fix this problem, the thing that solved it was getting the bitmap file without using the AsyncTask class.无论如何,我设法解决了这个问题,解决它的方法是在不使用 AsyncTask class 的情况下获取 bitmap 文件。

Code below:下面的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM