我不知道为什么 decodeFile
在 param 指向 Assets 文件夹中的文件时不起作用。
// Load images from the file path
String[] dir = null;
try {
dir = GenericMainContext.sharedContext.getAssets().list("drawable");
// dir Log => [ic.png, ic_info_dark.png, ic_launcher_default.png]
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (String uri : dir){
// do your stuff here
if (uri!=null) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeFile("file:///android_asset/drawable/"+uri); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
有什么解释吗?
最佳答案
file:///android_asset/
被WebView用来加载assets。要以编程方式加载 Assets ,请使用 getAssets()
方法,该方法返回 Context
对象(例如 Activity
)上的 AssetMAnager
. open(filename)
会将 InputStream
返回到您的 Assets 文件。以下将从您的 Assets 文件 fileName
在 Activity
中创建一个 Bitmap
,
BitmapFactory.decodeStream(getAssets().open(fileName))
关于android - Assets 中的 BitmapFactory.decodeFile 不起作用(FileNotFindException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31294162/