我需要从资产加载图像,以避免在某些特定情况下调整POT图像的froyo 2.2.2错误.避免它的方法是从资源目录加载图像文件.
我正试着这样做:
String imagePath = "radiocd5.png";
AssetManager mngr = context.getAssets();
// Create an input stream to read from the asset folder
InputStream is=null;
try {
is = mngr.open(imagePath);
} catch (IOException e1) { e1.printStackTrace();}
//Get the texture from the Android resource directory
//InputStream is = context.getResources().openRawResource(R.drawable.radiocd5);
Bitmap bitmap = null;
try {
//BitmapFactory is an Android graphics utility for images
bitmap = BitmapFactory.decodeStream(is);
} finally {
//Always clear and close
try {
is.close();
is = null;
} catch (IOException e) {
}
}
Run Code Online (Sandbox Code Playgroud)
但我在线上得到NullPointerException is.close();
我捕获了一个FileNotFoundException:radiocd5.png,但该文件位于我的资产目录中:S
我做得不好?该文件被调用radiocd5.png,它在assets目录中
Pet*_*ton 67
您可以按照我在资产中显示数据的教程进行操作:https
://xjaphx.wordpress.com/2011/10/02/store-and-use-files-in-assets/包含要加载的图像和要显示的文本的示例.
我现在添加了所提供链接的相关部分(如遇地震或其他事情);-)泰丰
// load image
try {
// get input stream
InputStream ims = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
}
catch(IOException ex) {
return;
}
Run Code Online (Sandbox Code Playgroud)
不使用资产目录,而是将文件放入 /res/raw,然后您可以使用以下 URI 访问它: android.resource://com.your.packagename/" + R.raw.radiocd5
| 归档时间: |
|
| 查看次数: |
86945 次 |
| 最近记录: |