1

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.

enter image description here

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 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@M D. Thanks for your reply. But you have note that I am loading the images from my folder in my android device and that the image name will be dynamically stored as Image 1,2, and so(incrementing the counter value,also uses shared preferences to store the last value). If this is the case, then how would I be able to achieve File imgFile = new File( "/data/data/package/app_my_sub_dir/images/" + file_name); Do I need to get the last stored count value and set accordingly ?
@San this url used my me. It's my requirement but You can add your url into this. Cutomized by your way.
@M D. I dont mean the URL in file but a query about how do I set the image name to it ? Thanks in advance.
@San in that case you can create a String ArrayList and add images name one by one into this arraylist and you can pass this image name into my "ImgBitFromFile(String file_path,String imagename)". Get it
I guess the following is what you mean. int count = 20; for(int i =1;i<count;i++){ file_name = "Image " + i + ".jpg" } and passing the file name to ImgBitFromFile(String file_path,String imagename). Is that it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.