【问题标题】:Camera Activity returns null URI相机活动返回空 URI
【发布时间】:2014-09-22 14:24:32
【问题描述】:

我正在三星 Note 上测试我的应用, 这是我以前调用相机的方式

Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, CAMERA_CAPTURE);

然后在活动结果中:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK)
    {
        if(requestCode == CAMERA_CAPTURE)
        {
            picUri = data.getData();
            //continue my code
        }
    }
}

但后来我最近将测试更改为 Galaxy Nexus,我注意到 data.getData() 现在返回 null 而不是临时图像 uri。 我阅读了一些建议,并且都提到将文件传递给相机活动作为新 iamge 的位置。 现在我将代码更改为:

....
File photoFile = createImageFile();
//photoFile = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(captureIntent, CAMERA_CAPTURE);
....

使用方法

private File createImageFile() throws IOException
{
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    //File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "alboom");
    //File storageDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "alboom");
    //File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    //File storageDir = getFilesDir();  //not working
    //File storageDir = getDir("mydir", Context.MODE_PRIVATE);  //not working
    //File storageDir = getCacheDir();  //not working

    if (!storageDir.mkdirs())   //required if creating a new dir
        Log.e(TAG, "Directory not created");

    //File image = File.createTempFile( imageFileName,  ".jpg", storageDir );
    File image = new File(storageDir, imageFileName);   
    //File image = File.createTempFile(imageFileName, ".jpg");  //not working


    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

以及结果处理

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK)
    {
        if(requestCode == CAMERA_CAPTURE)
        {
            picUri = Uri.fromFile(photoFile);
            //continue my code
        }
    }
}

现在我的代码运行良好,但只有当我写入外部存储时.. 内部存储总是不会从相机活动中返回。

我的问题是:

  1. 为什么我不能写入内部存储?
  2. 如何将“未隐藏”的图像文件写入外部存储?
  3. 为什么旧代码停止工作?

【问题讨论】:

  • "为什么我不能写入内部存储?" -- 因为相机应用无法访问您应用的内部存储空间。
  • 是的。当您传递 EXTRA_OUTPUT 时,三星相机应用程序从 android 4.3 开始返回 null。但是你并不真的需要 data.getData() Uri,因为你已经知道 Uri 是什么
  • 感谢@CommonsWare
  • @CommonsWare 感谢您的解释......此外,我注意到写入外部存储会写入隐藏文件,有没有办法写入不隐藏的文件
  • “我注意到写入外部存储会写入一个隐藏文件”——我不明白你的意思,抱歉。

标签: android file android-activity camera storage


【解决方案1】:

有时,尽管写了这样的东西:

File source = null;
source = new File(imageUri.getPath());

或:

Uri url= Uri.parse(stringPath);

...您的 uri 可能会获得一个空值。它似乎与最后一个 android 版本(4.4.4)的错误有关。那该怎么办?一个技巧是强制保存新图像以刷新路径:

 Uri tempUri = getImageUri(getApplicationContext(), imageBitmap);

(...)

public Uri getImageUri(Context inContext, Bitmap inImage) {
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  return Uri.parse(path);
}

试试吧!你也可以take a look at this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多