我一直在尝试制作一个小画廊,而位图总是返回 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卡里。我检查了图像的路径,它是正确的。这里做错了什么?