Android NO Image written on the specified MediaStore.EXTRA_OUTPUT URI

553 views Asked by At

In my application ImageTakerActivity creates MediaStore.ACTION_IMAGE_CAPTURE intent to capture image.

I specified the following URI as MediaStore.EXTRA_OUTPUT. So its expected that ACTION_IMAGE_CAPTURE will write the taken photo to the corresponding file represented in the URI.

                 /******** ImageTakerActivity *********/

private void captureImage()
{
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri();

    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    takePictureIntent.putExtra(MediaStore.EXTRA_FULL_SCREEN, true);

    if (takePictureIntent.resolveActivity(getPackageManager()) != null)
    {
        startActivityForResult(takePictureIntent, REQUEST_CODE_TAKE_IMAGE);
    }
}

public Uri getOutputMediaFileUri()
{
    return Uri.fromFile(getOutputMediaFile());
}


private File getOutputMediaFile()
{
    // External sdcard location
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),IMAGE_DIRECTORY_NAME);

    //Deleting previous Images
    if(mediaStorageDir.exists())
    {
        DeleteRecursive(mediaStorageDir);
    }

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists())
    {
        if (!mediaStorageDir.mkdirs())
        {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmssSSS",Locale.getDefault()).format(new Date());
    File mediaFile = new File(mediaStorageDir.getAbsolutePath() + File.separator + "IMG_" + timeStamp + ".jpg");

    return mediaFile;
}

In ActivityResult I did the following to retrieve the saved image:

private void decodeImageStream()
{
    InputStream stream = null;
    try
    {
        //this fileUri was passed to the takePictureIntent
        stream =  getContentResolver().openInputStream(fileUri);

        image = BitmapFactory.decodeStream(stream);
    } 
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if(stream != null)
        {
            try
            {
                stream.close();
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}

Most of the time expected image is successfully decoded in image variable.

But sometimes after taking the picture when i tap OK, whole screen does a quick rotation and becomes black. After returning to onActivityResult of ImageTakerActivity, I get nothing in the image variable! Also there is no Exception on BitmapFactory.decodeStream too.

I have tried reading various articles available, but I don't know how to resolve the issue. Any help is very much appreciated!

0

There are 0 answers