personne3000 personne3000 - 1 month ago 8x
Android Question

Picture file captured with camera intent empty for a while in onActivityResult

I am having a very strange bug trying to take a picture via intent.
Activity code (a little simplified):

private void startCapture() throws IOException {
// Create output file
final File photoFile = makePhotoFile();
_photoPath = photoFile.getAbsolutePath();

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
* As of Android N (which went out shortly after I posted this question),
* you cannot use Uri.fromFile this way anymore.
* You should use a FileProvider. */
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));

if (cameraIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(cameraIntent, REQUEST_CODE_IMAGE_CAPTURE);
else {

private File makePhotoFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
String imageFileName = "MyPhoto_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
return File.createTempFile(imageFileName, ".jpg", storageDir);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(resultCode == RESULT_OK) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
final byte[] buffer = new byte[2048];
int read;
byte[] photoBytes;
try(FileInputStream fis = new FileInputStream(_photoPath)) {

for(int i=0; i<10; i++) { // Always working after first iteration though
if( (read = <= 0) {
// Why does this get printed once ??
Log.w("", "Empty for now...");
else {
Log.w("", "Working now !");
bos.write(buffer, 0, read);

while((read = >= 0) {
bos.write(buffer, 0, read);

photoBytes = bos.toByteArray();
} // Catch clauses removed for simplicity

// Everything working OK after that (photoBytes decodes fine with the BitmapFactory)

And the log output:

03-01 16:32:34.139 23414-23414/ W/
Empty for now... 03-01 16:32:34.139 23414-23414/
W/ Working now !

As you can see, in the onActivityResult after taking the picture, the file is empty for the first call to And then it can be read correctly !
I thought that when the camera intent returns to the calling activity, the file would be already written. Is there some kind of delay ? I am also surprised to see that it is always working after exactly one iteration in the for loop.

Note that if I put a breakpoint at the beginning of onActivityResult, it delays execution a bit, and everything works normally (the file is read correctly at the first try).

I am using a stock Nexus 6P under Android 6.0.1 with the stock camera app.

Any ideas ?

Important edit: As for Android N, you should use a FileProvider instead of Uri.fromFile as I did before. See this blog post and these explanations from the Android team.


File.createTempFile. Do not create that file already. The only thing you need is a file name. A file path. The Camera app will create the file. So rename your function to create new file name.