【问题标题】:Android: decodeFile always returns null for file in internal storageAndroid:decodeFile 总是为内部存储中的文件返回 null
【发布时间】:2011-08-26 03:54:16
【问题描述】:

我有一个文件本地保存到应用程序的私有存储中。我已经验证它存在,但是每当我调用BitmapFactory.decodeFile 它总是返回null

如果我将文件保存为资源并使用ImageView.setImageResource,它总是显示得很好。

有什么问题?

这里是sn-p:

filename = "test.png";

if (doesFileExist(filename))
    Bitmap bMap = BitmapFactory.decodeFile(filename);

我也试过了:

Bitmap bMap = BitmapFactory.decodeFile(getFilesDir().getPath()
                    + filename);

【问题讨论】:

  • 在 decodeFile() 中,您必须传递文件的完整 pah,而不仅仅是名称或您尝试的方式

标签: android bitmapfactory


【解决方案1】:

这个问题之前已经回答过,比如这里: BitmapFactory.decodeFile returns null even image exists

这正是我所需要的:

String fname=new File(getFilesDir(), "test.png").getAbsolutePath();

【讨论】:

  • 你应该回答你自己的问题。我遇到了同样的问题,并从你那里得到了解决方案。您引用的问题也没有在我最初的搜索中。干杯!
【解决方案2】:

不要使用 BitmapFactory.decodeFile,而是尝试使用 InputStream:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    if(resultCode == RESULT_OK){          
        Uri selectedImage = imageReturnedIntent.getData();
        InputStream imageStream = getContentResolver().openInputStream(selectedImage);
        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);

【讨论】:

    【解决方案3】:

    伙计们,应用程序资源中存储的文件应该以特殊方式引用。例如。如果文件位于资产中并命名为“myfile.png”,则必须将其引用为:

    String uriString="file:///android_asset/myfile.png";
    Uri uri=Uri.parse(uriString);
    

    【讨论】:

    • 好消息,但在这种情况下,文件被下载到应用程序的私有存储中,而不是在资源中。
    【解决方案4】:

    BitmapFactory.decodeFile 需要一个没有该方案的文件路径。 IE。开头没有file://

    如果你正在处理一个 Uri,不要只是 .toString() 它,而是在它上面调用 .getPath(),并将它传递给方法。

    【讨论】:

      【解决方案5】:

      添加所需权限后(READ_EXTERNAL_STORAGE、WRITE_EXTERNAL_STORAGE)

      您需要将此行添加到清单中: android:requestLegacyExternalStorage="true"

      【讨论】:

        【解决方案6】:

        你能试试 fileList() 吗?它

        返回一个字符串数组,命名为 与此相关的私人文件 Context的应用包。

        【讨论】:

        • 实际上,我用它来验证文件在 Logcat 输出中的作用。所以,文件就在那里。
        【解决方案7】:

        对我来说,我从本地保存的 URL 获取图像,例如“file:///storage/emulated/0/....”(我使用 Phonegap 插件来捕获图像。插件给了我图像路径,它我需要在本机代码中使用)

        这是对我有用的代码 sn-p。

        String captured_image_info = "file:///storage/emulated/0/Android/data/com.testapp/cache/1493809796526.jpg"
        Uri uri=Uri.parse(captured_image_info);
        largeLog("uri", "" + uri);
        
        InputStream imageStream = getContentResolver().openInputStream(uri);
        
        Bitmap bm = BitmapFactory.decodeStream(imageStream);
        
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
        
        byte[] decodedBytes = baos.toByteArray();
        
        Bitmap img_captured_image = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
        

        【讨论】:

          【解决方案8】:

          您需要为 Manifest.permission.READ_EXTERNAL_STORAGE 设置运行时权限 如果你不这样做,你会得到一个空值,而不会在日志中得到任何错误消息甚至指示。 请注意,您需要在 Manifest 和运行时请求权限。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-11-01
            • 1970-01-01
            • 2022-10-14
            • 1970-01-01
            • 1970-01-01
            • 2017-11-21
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多