[android-developers] Re: Get image URI from camera?
Just FYI, this bug might be relevant: http://code.google.com/p/android/issues/detail?id=1480 Basically, the existing MediaStore.ACTION_IMAGE_CAPTURE intent is probably all what you need, the only problem is the returned image is not full-sized. BoD On May 7, 4:37 pm, Anna PS annapowellsm...@googlemail.com wrote: Hi Mark, I've used thecameracode from the Advanced Android book and it's working nicely. Thank you. Just a couple of basic questions (for Mark, or anyone else) about the retrieval/URIissue, both assuming the photo is saved on the SD card as photo.jpg. 1. Thumbnail display. When I go back to the first screen of the application, I want to show an ImageView of the jpeg that the user has just taken. Current code, which doesn't work because it's not looking in the SD card: bmp = BitmapFactory.decodeStream(openFileInput(photo.jpg)); iv.setImageBitmap(bmp); This gives the error: java.IO.FileNotFoundException: /data/data/ com.android.filename/files/photo.jpg. How can I ask BitmapFactory to look in the SD card for the jpeg? 2. Uploading the jpeg - I would like to convert the jpeg to a byte[] array, to upload it as part of a FilePart. Again, how can I retrieve the jpeg from the SD card to do this? My previous code used a mediaURIto retrieve a bitmap, compress it into a jpeg and write it to a byte array. This worked, but now I need to replace this with a reference to photo.jpg: Bitmap bitmap; ByteArrayOutputStream imageByteStream; byte[] imageByteArray = null; bitmap = android.provider.MediaStore.Images.Media.getBitmap (getContentResolver(),uri); imageByteStream = new ByteArrayOutputStream(); if (bitmap == null) { Log.d(LOG_TAG, No bitmap); } // Compress bmp to jpg, write to the byte output stream bitmap.compress(Bitmap.CompressFormat.JPEG, 80, imageByteStream); // Turn the byte stream into a byte array imageByteArray = imageByteStream.toByteArray(); Thanks again, Anna On May 7, 1:14 am, Mark Murphy mmur...@commonsware.com wrote: Anna PS wrote: What I don't know is what to launch when the user clicks on it. A modified version of theCamera class, or something like MediaStore.ACTION_IMAGE_CAPTURE? I haven't used the latter, so I can't comment on it. I need to (a) display the photo as a thumbnail on the activity's home screen, and (b) upload the photo as part of a multipart message. In both cases I need some way to refer back to the photo that the user has taken. I'm assuming the only way is via aURI? Is there another way? In terms of the thumbnail, you can either let Android scale it automatically from a file or use Bitmap to scale it under your control and hand the Bitmap to the ImageView. Neither of those requires aURI. I don't know how you intend to send the email and therefore what might be required for it. -- Mark Murphy (a Commons Guy)http://commonsware.com|http://twitter.com/commonsguy Android App Developer Books:http://commonsware.com/books.html --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get image URI from camera?
Is it possible to share the code! I am creating also such application where i wanted to take a picture and upload this picture to my website! Can I take a picture with the emulator? Thank you, Wouter On May 8, 12:30 am, Anna PS annapowellsm...@googlemail.com wrote: 1. Thumbnail display. When I go back to the first screen of the application, I want to show an ImageView of the jpeg that the user has just taken. Current code, which doesn't work because it's not looking in the SD card: bmp = BitmapFactory.decodeStream(openFileInput(photo.jpg)); iv.setImageBitmap(bmp); This gives the error: java.IO.FileNotFoundException: /data/data/ com.android.filename/files/photo.jpg. How can I ask BitmapFactory to look in the SD card for the jpeg? Use regular Java I/O to open the stream, rather than openFileInput(). Environment.getExternalStorageDirectory() will return you a File pointing to the SD card, which you can use as the basis for building your path. Thanks. For reference, the working code is: FileInputStream fstream = null; fstream = new FileInputStream (Environment.getExternalStorageDirectory() + / + photo.jpg); bmp = BitmapFactory.decodeStream(fstream); iv.setImageBitmap(bmp); 2. Uploading the jpeg - I would like to convert the jpeg to a byte[] array, to upload it as part of a FilePart. Again, how can I retrieve the jpeg from the SD card to do this? Use the same stream as above, and read it in: http://www.exampledepot.com/egs/java.io/File2ByteArray.html Works perfectly - thanks! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get image URI from camera?
Hi Mark, I've used the camera code from the Advanced Android book and it's working nicely. Thank you. Just a couple of basic questions (for Mark, or anyone else) about the retrieval/URI issue, both assuming the photo is saved on the SD card as photo.jpg. 1. Thumbnail display. When I go back to the first screen of the application, I want to show an ImageView of the jpeg that the user has just taken. Current code, which doesn't work because it's not looking in the SD card: bmp = BitmapFactory.decodeStream(openFileInput(photo.jpg)); iv.setImageBitmap(bmp); This gives the error: java.IO.FileNotFoundException: /data/data/ com.android.filename/files/photo.jpg. How can I ask BitmapFactory to look in the SD card for the jpeg? 2. Uploading the jpeg - I would like to convert the jpeg to a byte[] array, to upload it as part of a FilePart. Again, how can I retrieve the jpeg from the SD card to do this? My previous code used a media URI to retrieve a bitmap, compress it into a jpeg and write it to a byte array. This worked, but now I need to replace this with a reference to photo.jpg: Bitmap bitmap; ByteArrayOutputStream imageByteStream; byte[] imageByteArray = null; bitmap = android.provider.MediaStore.Images.Media.getBitmap (getContentResolver(), uri); imageByteStream = new ByteArrayOutputStream(); if (bitmap == null) { Log.d(LOG_TAG, No bitmap); } // Compress bmp to jpg, write to the byte output stream bitmap.compress(Bitmap.CompressFormat.JPEG, 80, imageByteStream); // Turn the byte stream into a byte array imageByteArray = imageByteStream.toByteArray(); Thanks again, Anna On May 7, 1:14 am, Mark Murphy mmur...@commonsware.com wrote: Anna PS wrote: What I don't know is what to launch when the user clicks on it. A modified version of the Camera class, or something like MediaStore.ACTION_IMAGE_CAPTURE? I haven't used the latter, so I can't comment on it. I need to (a) display the photo as a thumbnail on the activity's home screen, and (b) upload the photo as part of a multipart message. In both cases I need some way to refer back to the photo that the user has taken. I'm assuming the only way is via aURI? Is there another way? In terms of the thumbnail, you can either let Android scale it automatically from a file or use Bitmap to scale it under your control and hand the Bitmap to the ImageView. Neither of those requires aURI. I don't know how you intend to send the email and therefore what might be required for it. -- Mark Murphy (a Commons Guy)http://commonsware.com|http://twitter.com/commonsguy Android App Developer Books:http://commonsware.com/books.html --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get image URI from camera?
1. Thumbnail display. When I go back to the first screen of the application, I want to show an ImageView of the jpeg that the user has just taken. Current code, which doesn't work because it's not looking in the SD card: bmp = BitmapFactory.decodeStream(openFileInput(photo.jpg)); iv.setImageBitmap(bmp); This gives the error: java.IO.FileNotFoundException: /data/data/ com.android.filename/files/photo.jpg. How can I ask BitmapFactory to look in the SD card for the jpeg? Use regular Java I/O to open the stream, rather than openFileInput(). Environment.getExternalStorageDirectory() will return you a File pointing to the SD card, which you can use as the basis for building your path. 2. Uploading the jpeg - I would like to convert the jpeg to a byte[] array, to upload it as part of a FilePart. Again, how can I retrieve the jpeg from the SD card to do this? Use the same stream as above, and read it in: http://www.exampledepot.com/egs/java.io/File2ByteArray.html -- Mark Murphy (a Commons Guy) http://commonsware.com _The Busy Coder's Guide to Android Development_ Version 2.0 Available! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get image URI from camera?
1. Thumbnail display. When I go back to the first screen of the application, I want to show an ImageView of the jpeg that the user has just taken. Current code, which doesn't work because it's not looking in the SD card: bmp = BitmapFactory.decodeStream(openFileInput(photo.jpg)); iv.setImageBitmap(bmp); This gives the error: java.IO.FileNotFoundException: /data/data/ com.android.filename/files/photo.jpg. How can I ask BitmapFactory to look in the SD card for the jpeg? Use regular Java I/O to open the stream, rather than openFileInput(). Environment.getExternalStorageDirectory() will return you a File pointing to the SD card, which you can use as the basis for building your path. Thanks. For reference, the working code is: FileInputStream fstream = null; fstream = new FileInputStream (Environment.getExternalStorageDirectory() + / + photo.jpg); bmp = BitmapFactory.decodeStream(fstream); iv.setImageBitmap(bmp); 2. Uploading the jpeg - I would like to convert the jpeg to a byte[] array, to upload it as part of a FilePart. Again, how can I retrieve the jpeg from the SD card to do this? Use the same stream as above, and read it in: http://www.exampledepot.com/egs/java.io/File2ByteArray.html Works perfectly - thanks! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get image URI from camera?
Anna PS wrote: I need to start the Camera activity from within my application, take a picture, and return the photo URI to another activity. Surely this should be straightforward? But the Camera API sample code doesn't show how to get the URI. I can't find the answer on this forum, either. Since the Camera activity could be used to take one or 100 pictures, I would be surprised if it returned a result. The Camera *class* (Camera API) gives you a JPEG file as a byte array. It is up to you to store that someplace (e.g., SD card, Web service). -- Mark Murphy (a Commons Guy) http://commonsware.com | http://twitter.com/commonsguy _The Busy Coder's Guide to Android Development_ Version 2.0 Available! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get image URI from camera?
I need to start the Camera activity from within my application, take a picture, and return the photo URI to another activity. Ugh. The more I search for answers, the more baffled I get... Here's what I need to do: - From the home screen, have a big button saying Click here to take a photo - Show a camera preview screen, let the user take a photo - Go back to the home screen, display the photo as a thumbnail (using the photo URI, I guess) - If the user is happy, upload the photo as a multipart message My app is mostly complete, but I'm really struggling to know how to use the Camera API, and how to get something as simple as a URI back from it. I'm writing an open source, useful application for a charity. Does anyone have any sample code that might help me? Google people - it would really help to have some sample code for how to do this - I can see from the forums that it's a common problem :( Anna --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get image URI from camera?
Anna PS wrote: Ugh. The more I search for answers, the more baffled I get... Here's what I need to do: - From the home screen, have a big button saying Click here to take a photo You might be able to do that with an AppWidget in 1.5, but otherwise, there is no means of doing a big button on the home screen. - Show a camera preview screen, let the user take a photo OK. - Go back to the home screen, display the photo as a thumbnail (using the photo URI, I guess) At this point, I am hoping that by home screen you really mean the first activity in your application. - If the user is happy, upload the photo as a multipart message OK. My app is mostly complete, but I'm really struggling to know how to use the Camera API, and how to get something as simple as a URI back from it. Again, there is no URI. It's not even clear why you want one for your given circumstance. -- Mark Murphy (a Commons Guy) http://commonsware.com | http://twitter.com/commonsguy _The Busy Coder's Guide to Android Development_ Version 2.0 Available! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get image URI from camera?
Anna PS wrote: Thanks Mark. I've tried to intercept the Camera class's onKeyDown event to save a picture and take the URI back to my home activity - code below. There is no onKeyDown() in the Camera class. http://developer.android.com/reference/android/hardware/Camera.html Unfortunately, this is crashing with Java.Lang.UnsupportedOperationException: Unknown URI: content://media/external/images/media. Can you advise? Anna public boolean onKeyDown(int keyCode, KeyEvent event) { ImageCaptureCallback iccb = null; Uri uri = null; if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { Log.e(LOG_TAG, KeyEvent.KEYCODE_DPAD_CENTER pressed); try { String filename = timeStampFormat.format(new Date()); ContentValues values = new ContentValues(); values.put(Media.TITLE, filename); values.put(Media.DESCRIPTION, Image capture by camera); uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); iccb = new ImageCaptureCallback(getContentResolver() .openOutputStream(uri)); } catch (Exception ex) { ex.printStackTrace(); Log.e(getClass().getSimpleName(), ex.getMessage(), ex); } } if (keyCode == KeyEvent.KEYCODE_BACK) { return super.onKeyDown(keyCode, event); } if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { camera.takePicture(mShutterCallback, mPictureCallbackRaw, iccb); Log.d(LOG_TAG, Taken picture from KeyEvent.KEYCODE_DPAD_CENTER, uri = + uri.toString()); Intent i = new Intent(CameraActivity.this, Home.class); i.setData(uri); startActivity(i); return true; } return false; } I don't know how this code is being executed. I don't know what ImageCaptureCallback is. And I'm still not 100% sure why you're wanting a URI out of the deal. For some sample Camera-using code, you can grab the source code to my Advanced Android book: http://commonsware.com/AdvAndroid/ I can tell you that your code above has two keyCode == KeyEvent.KEYCODE_DPAD_CENTER items, which seems a bit odd given the structure of the method. If I were in your shoes, and going only by the limited use case you documented. I would: -- Use the Camera API to get the byte array -- Write the byte array to a JPEG file on the SD card -- Use that file as the basis for your thumbnail -- Use that file as the attachment in your email -- Let the Media content provider deal with indexing your image on its own time, or delete the image once it's emailed if you no longer need it It may be you have other criteria that cannot be met by the above procedure, in which case I apologize. -- Mark Murphy (a Commons Guy) http://commonsware.com | http://twitter.com/commonsguy _The Busy Coder's Guide to Android Development_ Version 2.0 Available! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get image URI from camera?
- From the home screen, have a big button saying Click here to take a photo You might be able to do that with an AppWidget in 1.5, but otherwise, there is no means of doing a big button on the home screen. Sorry, my fault, I'm not being clear. What I mean is that I want to start my application and show a screen with a button that says Click here to take a photo. (There'll be other buttons asking for different info.) I don't mean the phone's home screen. I know how to create a button, obviously :) What I don't know is what to launch when the user clicks on it. A modified version of the Camera class, or something like MediaStore.ACTION_IMAGE_CAPTURE? - Show a camera preview screen, let the user take a photo OK. - Go back to the home screen, display the photo as a thumbnail (using the photo URI, I guess) At this point, I am hoping that by home screen you really mean the first activity in your application Yes, indeed, I do. - If the user is happy, upload the photo as a multipart message OK. My app is mostly complete, but I'm really struggling to know how to use the Camera API, and how to get something as simple as a URI back from it. Again, there is no URI. It's not even clear why you want one for your given circumstance. I need to (a) display the photo as a thumbnail on the activity's home screen, and (b) upload the photo as part of a multipart message. In both cases I need some way to refer back to the photo that the user has taken. I'm assuming the only way is via a URI? Is there another way? Sorry to be so clueless, just finding the whole Camera class very confusing. I do believe that this is a common problem. Thanks for your help. Anna --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get image URI from camera?
Thanks Mark (sorry to have two separate threads going on here). I'll take a look at the code in your book. If I succeed, I'll post my code here for others to use. Links from other readers also much appreciated. Best wishes, Anna On May 7, 1:03 am, Mark Murphy mmur...@commonsware.com wrote: Anna PS wrote: Thanks Mark. I've tried to intercept the Camera class's onKeyDown event to save a picture and take the URI back to my home activity - code below. There is no onKeyDown() in the Camera class. http://developer.android.com/reference/android/hardware/Camera.html Unfortunately, this is crashing with Java.Lang.UnsupportedOperationException: Unknown URI: content://media/external/images/media. Can you advise? Anna public boolean onKeyDown(int keyCode, KeyEvent event) { ImageCaptureCallback iccb = null; Uri uri = null; if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { Log.e(LOG_TAG, KeyEvent.KEYCODE_DPAD_CENTER pressed); try { String filename = timeStampFormat.format(new Date()); ContentValues values = new ContentValues(); values.put(Media.TITLE, filename); values.put(Media.DESCRIPTION, Image capture by camera); uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); iccb = new ImageCaptureCallback(getContentResolver() .openOutputStream(uri)); } catch (Exception ex) { ex.printStackTrace(); Log.e(getClass().getSimpleName(), ex.getMessage(), ex); } } if (keyCode == KeyEvent.KEYCODE_BACK) { return super.onKeyDown(keyCode, event); } if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { camera.takePicture(mShutterCallback, mPictureCallbackRaw, iccb); Log.d(LOG_TAG, Taken picture from KeyEvent.KEYCODE_DPAD_CENTER, uri = + uri.toString()); Intent i = new Intent(CameraActivity.this, Home.class); i.setData(uri); startActivity(i); return true; } return false; } I don't know how this code is being executed. I don't know what ImageCaptureCallback is. And I'm still not 100% sure why you're wanting a URI out of the deal. For some sample Camera-using code, you can grab the source code to my Advanced Android book: http://commonsware.com/AdvAndroid/ I can tell you that your code above has two keyCode == KeyEvent.KEYCODE_DPAD_CENTER items, which seems a bit odd given the structure of the method. If I were in your shoes, and going only by the limited use case you documented. I would: -- Use the Camera API to get the byte array -- Write the byte array to a JPEG file on the SD card -- Use that file as the basis for your thumbnail -- Use that file as the attachment in your email -- Let the Media content provider deal with indexing your image on its own time, or delete the image once it's emailed if you no longer need it It may be you have other criteria that cannot be met by the above procedure, in which case I apologize. -- Mark Murphy (a Commons Guy)http://commonsware.com|http://twitter.com/commonsguy _The Busy Coder's Guide to Android Development_ Version 2.0 Available! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get image URI from camera?
Anna PS wrote: What I don't know is what to launch when the user clicks on it. A modified version of the Camera class, or something like MediaStore.ACTION_IMAGE_CAPTURE? I haven't used the latter, so I can't comment on it. I need to (a) display the photo as a thumbnail on the activity's home screen, and (b) upload the photo as part of a multipart message. In both cases I need some way to refer back to the photo that the user has taken. I'm assuming the only way is via a URI? Is there another way? In terms of the thumbnail, you can either let Android scale it automatically from a file or use Bitmap to scale it under your control and hand the Bitmap to the ImageView. Neither of those requires a URI. I don't know how you intend to send the email and therefore what might be required for it. -- Mark Murphy (a Commons Guy) http://commonsware.com | http://twitter.com/commonsguy Android App Developer Books: http://commonsware.com/books.html --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---