java - 读取位图文件返回 null

标签 java android android-studio bitmap android-asynctask

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

以下是从 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);
    }

}

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

    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

知道我应该做什么吗?

最佳答案

您可以使用 next 读取原始文件。这对于我们的代码来说非常完美。

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

关于java - 读取位图文件返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58959771/

相关文章:

java - 如何解决 "The specified Gradle distribution ' https ://services. gradle.org/distributions/gradle-4.10.1-all.zip'不存在。”

java - 我可以使用重构来将一个变量的类型交换为另一个变量吗?

android - 从 Android Studio gradle build 访问环境变量

java - 使用 py4j 将 Log4j 连接到 java/python 项目中的 ipython 笔记本 stderr

javascript - 在不使用媒体插件的情况下在Android Cordova Project中播放音频

安卓 QN : Change color of paint without affect previously drawn lines

android - 检索对 Log 的调用?例如Log.w() Log.e() Log.i()

java - 获取服务器的日期或时间

java - 为什么该算法执行IF语句时没有错误?

java - 类型推断是否适用于 Java 非泛型