Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    /* IMPORTANT NOTE TO READERS
     * 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 {
        deleteCurrentPhoto();
    }
}

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);
}

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

    if(requestCode == REQUEST_CODE_IMAGE_CAPTURE) {
        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 = fis.read(buffer)) <= 0) {
                        // Why does this get printed once ??
                        Log.w("my.package.my.app", "Empty for now...");
                    }
                    else {
                        Log.w("my.package.my.app", "Working now !");
                        bos.write(buffer, 0, read);
                        break;
                    }
                }

                while((read = fis.read(buffer)) >= 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/my.package.my.app W/my.package.my.app: Empty for now... 03-01 16:32:34.139 23414-23414/my.package.my.app W/my.package.my.app: Working now !

As you can see, in the onActivityResult after taking the picture, the file is empty for the first call to FileInputStream.read... 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.


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.

like image 905
personne3000 Avatar asked Mar 01 '16 07:03

personne3000


1 Answers

Do not create that file already. Remove the File.createTempFile().

The only thing you need is a file name. A file path. The Camera app will create the file on its own. So change your function to create new file name. E.g. new File(storageDir, imageFileName + ".jpg")

like image 86
greenapps Avatar answered Oct 01 '22 10:10

greenapps