问题
I have a situation where I need to set a list of images and I am unable to do that cause I am not aware on how to pass a list of string array to a bitmap.
CODE :
private void setImageBit(String item, ImageView imageView){
path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Pictures" + File.separator + "SanPics2";
File file = new File(path,"Image 5.jpg");
String item = file.toString();
Bitmap bitmap = BitmapFactory.decodeFile(item);
float i = ((float) imageWidth) / ((float) bitmap.getWidth());
float imageHeight = i * (bitmap.getHeight());
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) imageView.getLayoutParams();
params.height = (int) imageHeight;
params.width = (int) imageWidth;
imageView.setLayoutParams(params);
imageView.setImageBitmap(bitmap);
}
I know for a fact that the above code would set only the specified "Image 5.jpg" but I would require more images to be set in a MASONRY VIEW. See below for example. I want to set images taken from a camera in the below depicted view dynamically. But I am unable to achieve that.

I have also tried decodeByteArray()
which would get array(I would pass the list of path in array) but that doesn't seem to work either cause converting string path to byte doesn't work as expected. And also that I have tried the following
path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Pictures" + File.separator + "SanPics2";
File file = new File(path);
Bitmap bitmap = BitmapFactory.decodeFile(path);
imageView.setImageBitmap(bitmap);
But this seems to return a empty set of images. A blank white page with no image. I would be glad to provide you with more code if required. Any help would be highly appreciated. Thanks in advance and feel free to ask for any clarifications.
回答1:
You just need to create ArrayList with Bitmap like below:
ArrayList<Bitmap> img_bitmap
and now, just load one by one images using below function and store it into bitmap arrylist like below:
public static Bitmap ImgBitFromFile(String file_name) {
File imgFile = new File(
"/data/data/package/app_my_sub_dir/images/" + file_name);
//System.out.println("Image Exists:::" + imgFile.getAbsolutePath().toString());
if (imgFile.exists()) {
// System.gc();
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
.getAbsolutePath());
//System.out.println("Image Exists:::");
return myBitmap;
}
return null;
}
and store into bitmap arraylist like below:
for (int i = 0; i < c.getCount(); i++) {
if(c.getString(3)!="") {
img_bitmap.add(ImgBitFromFile(c.getString(5)));//Image path from Database
img_name.add(c.getString(3));
}
c.moveToNext();
}
and at the end load one by one images into your grid view ImageAdapter.
来源:https://stackoverflow.com/questions/20987392/how-to-pass-images-into-arrayliststring-in-android