2

I need to pick an image from gallery and then convert it into byte data. I know how to pick image from gallery. Also I know how to convert image to byte data. But problem is i convert image that are in drawable but now I need to pick it from gallery and convert it to byte code. Any help THanks In onClick function I am using this code to pick image from gallery

Intent image = new Intent(Intent.ACTION_GET_CONTENT);
        image.setType("Image/*");
        startActivityForResult(image, 0);

And I have used following code to convert image that is in drawable to byte data.

 bm = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
       data = new ByteArrayOutputStream(); 
       bm.compress(Bitmap.CompressFormat.JPEG, 40 , data);
     bitmapdata = data.toByteArray();

Now how would i convert image from gallery to byte data. Thanks

nadeem gc
  • 484
  • 1
  • 8
  • 22
  • i didn't get any direction how would i do that. I can paste code of convert into byte data to an image that is in drawable. should i paste? – nadeem gc Oct 17 '12 at 19:06
  • You said: "*I know how to pick image from gallery.*" What is your method for doing this? – Cat Oct 17 '12 at 19:07
  • from drawable it is easy just i have to do R.drawable.name of image – nadeem gc Oct 17 '12 at 19:13
  • This one shows you how to get the path of the gallerty image in your storage: http://stackoverflow.com/questions/12123883/get-all-images-in-gallery – Tamir Scherzer Oct 17 '12 at 19:18
  • @Tamir In this example. can i use selectedImagePath in place of R.drawable.ic_launcher for conversion – nadeem gc Oct 17 '12 at 19:28
  • you need to read it from storage like in this: http://stackoverflow.com/questions/5209842/read-write-file-to-internal-private-storage – Tamir Scherzer Oct 17 '12 at 19:36
  • @Tamir thanks but i m confused. it will be really helpful if u paste the code related to my problem as an answer – nadeem gc Oct 17 '12 at 19:50
  • k, this one should be more helpful: http://www.java2s.com/Code/Android/2D-Graphics/SaveBitmaptoandloadfromExternalStorage.htm – Tamir Scherzer Oct 17 '12 at 19:54

1 Answers1

2

In onActivityResult you will receive the Uri to your selected image like this:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == PICK_IMAGE && data != null && data.getData() != null){
       Uri imageUri = data.getData();
       //....
    }
}

Then to retrieve it from the MediaStore you should use :

 Bitmap bitmap =
      MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);

after that, you should process the Bitmap like you do it now.

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • the code to retrieve will be in the onActivityResult method?? If i use this outside this method i get the error :::: - Default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor - Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor – nadeem gc Oct 18 '12 at 14:07