Android NO Image在指定的MediaStore.EXTRA_OUTPUT URI上写入

时间:2015-06-21 13:20:27

标签: android image android-camera android-camera-intent image-capture

在我的应用程序中ImageTakerActivity创建MediaStore.ACTION_IMAGE_CAPTURE意图来捕获图像。

我将以下URI指定为MediaStore.EXTRA_OUTPUT。因此,它期望ACTION_IMAGE_CAPTURE将拍摄的照片写入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;
}

ActivityResult中,我执行了以下操作来检索已保存的图像:

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

大部分时间预期的图像在image变量中成功解码。

但有时在点击OK后拍摄照片后,整个屏幕会快速旋转并变为黑色。返回onActivityResult的{​​{1}}后,ImageTakerActivity变量中什么都没有!此外,image也没有异常。

我试过阅读各种文章,但我不知道如何解决这个问题。非常感谢任何帮助!

0 个答案:

没有答案