11

In my application I want to upload the image.

For that I have to get images from gallery in android device.

How do I write code that accomplishes this?

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Aswan
  • 5,085
  • 10
  • 46
  • 70

1 Answers1

38

Raise an Intent with Action as ACTION_GET_CONTENT and set the type to "image/*". This will start the photo picker Activity. When the user selects an image, you can use the onActivityResult callback to get the results.

Something like:

Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK)
    {
        Uri chosenImageUri = data.getData();

        Bitmap mBitmap = null;
        mBitmap = Media.getBitmap(this.getContentResolver(), chosenImageUri);
        }
}
Samuh
  • 36,316
  • 26
  • 109
  • 116
  • 1
    hi samuh.by using this code i am getting image from media what's my problem is i need to store that image in remote server.i am converted mBitmap into bytearray when i try to send byte array it is shwoing 5 digit code i am feeling like it is address of the byte array .can you please help out from this problem Thanks in advance Aswan – Aswan Apr 27 '10 at 08:28
  • 1
    you are probably sending the HashCode of the byte array instead of the array elements. – Samuh Apr 27 '10 at 09:12
  • 1
    in = new FileInputStream("/sdcard/pictures/einstein3.jpg"); buf = new BufferedInputStream(in,2000); System.out.println("1.................."+buf); byte[] byteArray= new byte[buf.available()]; buf.read(byteArray); now i am sending "byteArray" to server that is not sending only hashcode sending .can you suggest me where i made mistake – Aswan Apr 28 '10 at 07:16
  • On Android 4.4 this now has unwanted behaviour (launch the recent documents screen). See this answer: http://stackoverflow.com/a/2507973/276949 – Martin Konecny May 26 '14 at 16:28