我的资产文件夹中有一张图片(welcome_image.jpg)。并使用此代码从资产中读取。
Bitmap bit = null;
String url = "/assets/welcome_image.jpg";
bit = BitmapFactory.decodeFile(url);
但是bit为null并且抛出错误为
03-26 16:42:15.303: D/Image exisits....(21547): Image url : /assets/welcome_image.jpg
03-26 16:44:39.853: E/BitmapFactory(21547): Unable to decode stream: java.io.FileNotFoundException: /assets/welcome_image.jpg: open failed: ENOENT (No such file or directory)
请告诉我如何实现这一目标。
答案 0 :(得分:1)
您可以使用AssetManager
使用InputStream
方法获取open()
,然后使用decodeStream()
BitmapFactory
方法获取位图。
private Bitmap getBitmapFromAsset(String strName)
{
AssetManager assetManager = getAssets();
InputStream istr = null;
try {
istr = assetManager.open(strName);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}
答案 1 :(得分:0)
您应该使用以下路径:
Bitmap bit = null;
bit = BitmapFactory.decodeFile("file:///android_asset/welcome_image.jpg");
答案 2 :(得分:0)
检查此代码
AssetManager assetManager = getAssets();
InputStream istr;
try {
istr = assetManager.open("ceo.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(istr);
imageView.setImageBitmap(bitmap);
istr.close();
} catch (IOException e) {
e.printStackTrace();
}
答案 3 :(得分:0)
试试如下:
InputStream bitmap=null; try { bitmap=getAssets().open("welcome_image.jpg"); Bitmap bit=BitmapFactory.decodeStream(bitmap); img.setImageBitmap(bit); } catch (IOException e) { e.printStackTrace(); } finally { if(bitmap!=null) bitmap.close(); }
答案 4 :(得分:0)
试试这个:
InputStream bitmap=null;
try {
bitmap=getAssets().open("welcome_image.jpg");
Bitmap bit=BitmapFactory.decodeStream(bitmap);
img.setImageBitmap(bit);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bitmap!=null)
bitmap.close();
}
答案 5 :(得分:0)
AssetManager assetManager = getAssets();
InputStream istr = assetManager.open(fileName);
Bitmap bitmap = BitmapFactory.decodeStream(istr);