簡體   English   中英

讀取 bitmap 文件返回 null

[英]Reading bitmap file returns null

我正在嘗試將 URL 中的文件保存為活動中的 bitmap 文件,然后從其他一些活動中讀取它並使用/顯示它。

這是從 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 class (帶有 SWITCH 語句的那個)似乎適用於下載和顯示圖像(例如:兩種情況“0”和“1”)。

第二個 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

還有一個奇怪的日志:

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

另外,文件路徑是:

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

知道我該怎么做嗎?

您可以使用 next 讀取原始文件。 這非常適合我們的代碼。

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

嗨,謝謝你們的快速回復!

抱歉,我忘了說,是的,該文件存在。

無論如何,我設法解決了這個問題,解決它的方法是在不使用 AsyncTask class 的情況下獲取 bitmap 文件。

下面的代碼:

                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