I am trying to take a picture with MediaStore.ACTION_IMAGE_CAPTURE intent in the following way:
String fileName = "testphoto.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image capture by camera");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
imageUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE)
.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI),
R.id.capture_image_request);
Handling activity result:
try {
uploadedPicture = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageUri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
And after that I'm trying to show just taken image:
((ImageView) findViewById(R.id.profile_picture_thumbnail)).setImageBitmap(uploadedPicture);
And that doesn't work. After that I tried to open an input stream from that Uri:
final InputStream is = getContentResolver().openInputStream(uri);
Log.("mydebug tag", is.available());
is.available() returns 0. Why is that file empty???
When I tried to debug I found that Uri like "content://media/external/images/media/68" had been returned. After that I hardcoded:
Uri uri = Uri.parse("content://media/external/images/media/68");
Bitmap pic;
try {
pic = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
((ImageView) findViewById(R.id.profile_picture_thumbnail)).setImageBitmap(pic);
} catch (FileNotFoundException e) {
Log.e(TAG, e.getMessage());
}
And that worked.
So I wonder why when I construct Uri manually - it works, but when I recieve a Uri of just taken picture - it doesn't? I have a feeling that when I access Uri of just taken picture, it isn't stored on a file system yet, that's why it's size is equal to 0 and can't be loaded into ImageView. But after a time (when I run my application again, with hardcoded Uri) file gets stored and works fine. Are there any ways to make a force flush of that file to a disk?