无法使用BitmapFactory.decodeFile读取图像

问题描述 投票:0回答:1

位图大小仅为49 KB。

我在AndroidManifest中拥有权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

但是以下代码无法从文件读取图像。

Im = BitmapFactory.decodeFile(file.absolutePath) // <== file: "/storage/emulated/0/Android/data/com.example.App1/files/MyImages/MyImg.png"

BitmapFactory.decodeFile返回null。

我使用Filestream搜索并尝试了如下建议:

try {
   // Img = BitmapFactory.decodeFile(file.absolutePath) //<= returns null

    val dest = File(fullPath, fileName)  // dest: "/storage/emulated/0/Android/data/com.example.App1/files/MyImages/MyImage.png"
    var fis: FileInputStream
    fis = FileInputStream(dest)      // <= Jumps to catch section from this line
    Img = BitmapFactory.decodeStream(fis)
}
catch (e: Exception) {
    Log.e("File could be find", e.message)    
}
return Img 

但是,当我调试它时,光标从fis = FileInputStream(dest)行跳到catch部分。

如何解决此问题?

android fileinputstream bitmapfactory
1个回答
0
投票

这给你带来什么

try {
   // Img = BitmapFactory.decodeFile(file.absolutePath) //<= returns null

    val dest = File(fullPath, fileName)  // dest: "/storage/emulated/0/Android/data/com.example.App1/files/MyImages/MyImage.png"

    System.out.println("DEST IS: " + dest);  // <= Check this in Logcat

    var fis: FileInputStream
    fis = FileInputStream(dest)      // <= Jumps to catch section from this line
    Img = BitmapFactory.decodeStream(fis)
}
catch (e: Exception) {
    Log.e("File could be find", e.message)    
}

© www.soinside.com 2019 - 2024. All rights reserved.