[英]bitmap always returning null in android
我一直在嘗試制作一個小型畫廊,並且位圖始終返回null。 代碼是這樣的:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
//Toast.makeText(getApplicationContext(),imgArray2.length+ " Image path from gallery : " + imgArray2[position], Toast.LENGTH_SHORT).show();
//Bitmap bitmap = BitmapFactory.decodeFile(imgArray2[position]);
//Uri uri = Uri.parse(imgArray2[position]);
//Bitmap bitmap = decodeFile(new File(uri.toString()).getAbsoluteFile());
//Bitmap bitmap = BitmapFactory.decodeFile(uri.toString());
//int imgID = getResources().getIdentifier(path, "drawable", "mypack.pack");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 15;
Bitmap bitmap = BitmapFactory.decodeFile(imgArray2[position], options);
//i.setImageResource(imgArray2[position]);
i.setImageBitmap(bitmap);
//Uri uri = Uri.parse(imgArray2[position]);
Toast.makeText(getApplicationContext(), "Image path from gallery : " + imgArray2[position], Toast.LENGTH_SHORT).show();
//i.setImageURI(Uri.parse(imgArray2[position]));
i.setLayoutParams(new Gallery.LayoutParams(170, 170));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
如您所注釋的代碼所示,我一直在嘗試許多選擇。 setImageURI
可以工作,但是我需要縮小圖像,因為我有很多。 圖像在sd卡中。 我檢查了圖像的路徑,它是正確的。 這是怎么了?
您是否分析過日志? 有時,當圖像太大而無法解碼時,Dalvik VM將拒絕內存請求,從而導致返回空圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.