我需要從資產中加載圖像以避免在某些特定情況下調整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) {
}
}
但我就行了is.close();
我捕捉FileNotFoundException異常得到NullPointerException異常:radiocd5.png,但該文件是在我的資產目錄:S
我在做什麼壞事?該文件稱爲radiocd5.png
,它是在assets
目錄而不是使用資產迪爾
我在做你的教程,爲什麼我會收到錯誤? – NullPointerException
什麼錯誤?請清楚,只要您提供給我足夠的信息,我會盡力幫助您。 –
我編輯我的問題,它給我錯誤is.close();行 – NullPointerException