【问题标题】:Path issue while saving and displaying an image in Android在 Android 中保存和显示图像时的路径问题
【发布时间】:2016-08-25 02:56:39
【问题描述】:

这可能是我在编写路径时的愚蠢,但这是我的代码,它无法找到配置的根目录。 getUriForFile 是导致错误的原因。

@Override
public void onClick(View v) {
    if (v.getId() == R.id.main_activity_camera_access_button) {
        Log.d(TAG, "clicked");
        Toast.makeText(getContext(), R.string.first_activity_toast_opening_camera, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if(intent.resolveActivity(getActivity().getPackageManager()) != null) {
            mPhotoFile = null;
            try {
                mPhotoFile = createImageFile();
            } catch (IOException ex) {
                Log.d(TAG, "exception while creating photo file");
            }
            if(mPhotoFile != null){
                mfileUri = FileProvider.getUriForFile(getContext(), "edu.lclark.imagineyourwall.fileprovider", mPhotoFile);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, mfileUri); // set the image file name

                // start the image capture Intent
                startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            }
        }
            //mfileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    }
}

我在 res 下有一个 xml 目录,其中有我的 my_paths.xml。这是 my_paths.xml 的代码。

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
    path="Android/data/edu.lclark.imagineyourwall.name/files/Pictures" />
</paths>

另外,我的清单看起来像这样,至少提供程序部分是这样的。

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="edu.lclark.imagineyourwall.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/my_paths"></meta-data>
    </provider>

【问题讨论】:

    标签: android path android-fileprovider


    【解决方案1】:

    我认为您正在关注 Android 开发人员文档,对吗?如果您的目标不是 Android N (Nougat),删除getUriForFile 行并将intent.putExtra(MediaStore.EXTRA_OUTPUT, mfileUri) 修改为intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile)) 是安全的。

    根据Android developer documentation;

    对于针对 Android N 及更高版本的最新应用,跨包边界传递 file:// URI 会导致 FileUriExposedException。因此,我们现在提出一种使用 FileProvider 存储图像的更通用的方法。

    【讨论】:

      猜你喜欢
      • 2011-06-20
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多