I found that this simple task was not at all clear for me in the documentation, nor did I found a complete guide to get this task done. I hope if people find the same problem, this article may help them.
Device
HTC Desire with Android 2.2.
Contents
1. Take picture and save it, along with a thumbnail
2. Show thumbnail
3. Show full size image
4. Sample project
Taking picture and saving it
Taking picture
I'm using the camera Intent to take the picture... No extra parameters passed.
// Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, TAKE_PICTURE); return true;
... and saving it
Closing the camera causes android to callback the onActivityResult method. I'm using a the static int TAKE_PICTURE=1 variable to identify the action that triggered this function request.
// //Called on intent return... used to grab pic @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == TAKE_PICTURE) { if (data != null) { //Full size pic stored automatically by the intent //lets grab the ContentProvider id for the image imageid = data.getData().getLastPathSegment(); //Set thumbnail IMAGE_ID value, holding the reference to the full size picture ContentValues values = new ContentValues(1); values.put(MediaStore.Images.Thumbnails.IMAGE_ID, imageid); //insert (thumbnail) record into MediaStore content provider Uri uri = getContentResolver().insert(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, values); //write the actual thumbnail to "disk" try { OutputStream outStream = getContentResolver().openOutputStream(uri); Bitmap mBitmap = data.getParcelableExtra("data"); //thumbnail mBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream); outStream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
Retrieve thumbnail
Just use the id from the code before to retrieve the thumbnail from MediaStore content provider.
Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), imageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
Show full size image
Using the same id...
// Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, ""+id); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(uri); startActivity(intent);
Sample Project
Download full source from here (Eclipse project).
Feedback
Please let me know if you found any errors in this code or problems with other devices.
6 comments :
wine.getImageId() gives me an error? Can you please post the declaration for wine.
Sorry Faheem,
That code is wrong... You should use the imageId (line 10 in the save picture method) instead of wine.getImageId()... evil copy/past.
The full source should be clear and working fine (I hope).
Sorry for the late reply.
kimile
When retrieving the thumbnail, where does the imageId come from? The imageid from the code is a String, but getThumbnail is looking for a long. This doesn't seem to be in the source code, either.
Hello thank you for your example,
i downloaded the project and this line is throwing an Exception
imageid = data.getDataString()().getLastPathSegment(); //returns full pic id
changing it to
imageid = data.getDataString(); //returns full pic id
makes it work partially, the show picture always says take picture firest
Thanks it helped a lot
Hi i am using the above code to open camera to capture image.
It is able to open camera successfully but onActivityResult the value of the intent is null and the application turns directly to RESULT_CANCELED.
How can I fix this issue please?
Thanks
Post a Comment