BitmapFactory.decodeFile 在资产中不起作用(FileNotFindException)
BitmapFactory.decodeFile in asset doesn't work (FileNotFindException)
我不知道为什么 decodeFile
在 参数 指向资产文件夹中的文件时不起作用。
// 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用来加载资源。要以编程方式加载资产,请使用 getAssets()
方法,该方法 return 是 Context
对象(例如 Activity
上的 AssetMAnager
。 open(filename)
将 return 一个 InputStream
到您的资产文件。以下将从您的资产文件 fileName
在 Activity
、
中创建一个 Bitmap
BitmapFactory.decodeStream(getAssets().open(fileName))
我不知道为什么 decodeFile
在 参数 指向资产文件夹中的文件时不起作用。
// 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用来加载资源。要以编程方式加载资产,请使用 getAssets()
方法,该方法 return 是 Context
对象(例如 Activity
上的 AssetMAnager
。 open(filename)
将 return 一个 InputStream
到您的资产文件。以下将从您的资产文件 fileName
在 Activity
、
Bitmap
BitmapFactory.decodeStream(getAssets().open(fileName))