2016-08-25 90 views
0

这可能是我在编写路径时的愚蠢,但这里是我的代码,它无法找到配置的根目录。 getUriForFile是导致错误的原因之一。在Android中保存并显示图像时出现路径问题

@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> 

回答

0

我认为您正在关注Android开发者文档以了解此权限吗?如果您不是针对Android N(牛轧糖),则可以安全删除getUriForFile行,并将intent.putExtra(MediaStore.EXTRA_OUTPUT, mfileUri)更改为intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile))

根据Android developer documentation;

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