21

I want to upload image from my phone gallery into my application .In my application there is button named upload. when i click button,it should move to gallery and in gallery if i select image that selected image should display as thumbnail in application.I want to upload 10 images, from gallery in my application.

user1083266
  • 1,751
  • 3
  • 24
  • 26

3 Answers3

28

On click of the gallery button, start startActivityForResult as follows:

startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY);

Consequently, detect GET_FROM_GALLERY (which is a static int, any request number of your choice e.g., public static final int GET_FROM_GALLERY = 3;) inside onActivityResult.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    //Detects request codes
    if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
        Uri selectedImage = data.getData();
        Bitmap bitmap = null;
        try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
        } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
    }
}
Dhruv Gairola
  • 9,102
  • 5
  • 39
  • 43
  • here we can upload only one image but i want to upload 10 images simultaneously.now again when i click upload button it should go to gallery and display image beside the previously displaying preview image – user1083266 Feb 02 '12 at 06:50
  • Hey Dhruv how do you display a thumbnail of it? – Lion789 Dec 23 '13 at 04:19
  • This has worked like a charm. I don't know why questioner didn't choose yet. – Vijay Kumar Kanta Dec 09 '17 at 11:41
  • `startActivityForResult` is deprecated. There is an alternative way of achieving this [here](https://www.geeksforgeeks.org/how-to-select-an-image-from-gallery-in-android/) (scroll below). Works like a charm – Clay Jun 13 '23 at 00:38
8

To view gallery:

Intent intent = new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_GET_CONTENT);
  startActivityForResult(Intent.createChooser(intent, "Select Picture"),REQUEST_CODE);

and to use it in your app:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  try {
   switch (requestCode) {

   case REQUEST_CODE:
    if (resultCode == Activity.RESULT_OK) {
     //data gives you the image uri. Try to convert that to bitmap
     break;
    } else if (resultCode == Activity.RESULT_CANCELED) {
     Log.e(TAG, "Selecting picture cancelled");
    }
    break;
   }
  } catch (Exception e) {
   Log.e(TAG, "Exception in onActivityResult : " + e.getMessage());
  }
 }
user229044
  • 232,980
  • 40
  • 330
  • 338
Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
  • I treid this code but i need to upload multiple images simultaneously.Also, i need to get thumbnail preview of images simultaneously. – user1083266 Feb 02 '12 at 06:25
5

This is the way to go:

startActivityForResult(
  new Intent(
    Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI
  ),
  GET_FROM_GALLERY
);
Nic Nilov
  • 5,056
  • 2
  • 22
  • 37
shafiullah
  • 51
  • 1
  • 1