Image file taken to hold the camera for some time in onActivityResult

I have a very strange error trying to take a picture using intent. Operation code (slightly 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 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 onActivityResult after the snapshot, the file is empty for the first call to FileInputStream.read ... And then it can be read correctly! I thought that when the camera’s intention returns to the defiant activity, the file will already be written. Is there any delay? I am also surprised to see that it only works after one iteration in the for loop.

, onActivityResult, , ( ).

Nexus 6P Android 6.0.1 .


: Android N, FileProvider Uri.fromFile, . . Android.

+5
1

. File.createTempFile().

, , . . . , . , new File(storageDir, imageFileName + ".jpg")

+3

All Articles