Reputation: 271
I trying to display a captured image with Glass Camera and display it in an ImageView.
That's what I do right now:
public void startCamera()
{
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, 100);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == RESULT_OK && null != data)
{
String photoPath = data.getExtras().getString("picture_file_path");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeFile(photoPath, options);
mImageView.setImageBitmap(bitmap);
}
}
However the bitmap is null. When I log the photoPath it gives me a file path like:
/mnt/sdcard/DCIM/Camera/20131216_195521_665.jpg
Any ideas?
Upvotes: 1
Views: 1526
Reputation: 6429
Due to processing that occurs on Glass after you take a photo, the file may not be completely written at the time onActivityResult
is called.
You should use a FileObserver
as described in the CameraManager
javadoc to defer your processing until after the file is ready. To do this, have the FileObserver
observe the parent directory of the given path for a CLOSE_WRITE
event on the file itself. An example of this is provided at the bottom of the page in our camera developer guide.
Upvotes: 2
Reputation: 1427
As Tony Allevato said the problem is that your program does not wait long enough for the file to appear. When I was experimenting with this on XE11 it would often take 5-10 seconds for the file to be readable in the file system. For some reason a FileObserver didn't work for me either, so I implemented a timer, and then I wrote my own activity.
We are hearing that the next release of the GDK will make the snapshot available, so your problem may go away then as you can use that. In the meantime, you might want to consider writing your own activity to take the picture, as I did here:
https://github.com/dazza222/GlassCameraSnapshot
Upvotes: 1