-1

My app contains a section where I have to load images from gallery and show them there. Please guide me how I can do that. I also want to make folders in which the photos will be placed. Thanks in advance for your help!

Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery,PICK_IMAGE);

I've tried the above code it selects the image and shows it on my app but when I restart my app the image disappears.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
        imageUri = data.getData();
        imageView.setImageURI(imageUri);
    }
}
Chandan Sharma
  • 2,803
  • 1
  • 17
  • 25
Saad Iqbal
  • 98
  • 4

3 Answers3

2
 final Uri imageUri = data.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);

                Bitmap newPicture;

                BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                bmOptions.inJustDecodeBounds = true;

                // Decode the image file into a Bitmap sized to fill the View
                bmOptions.inJustDecodeBounds = false;
                bmOptions.inPurgeable = true;

                newPicture = BitmapFactory.decodeStream(imageStream);


                myPicturesArrayList.add(newPicture);

                picturesAdapter = new MyPicturesAdapter(myPicturesArrayList); //Your custom adapter
                myRecyclerView.setAdapter(picturesAdapter );

Juan Sancho
  • 321
  • 1
  • 7
1

In that case you have to save the images first in your database locally or on server...and next time fetch from database

Amit B
  • 11
  • 1
1

After you load the image from gallery, save the image Uri in SharedPreferences. Next time load it directly (Get the uri from the Sharedpreference). For details pls refer Android Shared preferences example

Pradeep
  • 261
  • 2
  • 7