2017-08-16 178 views
1

我试图将图像保存到内部存储和我面对:安卓:FileProvider抛出:IllegalArgumentException无法找到配置的根包含/数据/数据/

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.example/app_images/f9732a4a-a22e-4b62-b9b3-e546c8edc93c.jpg. 
     at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711) 
     at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400) 
     at com.example.example.fragments.PhotosFragment.createImageFile(PhotosFragment.java:182) 
     at com.example.example.fragments.PhotosFragment.dispatchTakePictureIntent(PhotosFragment.java:191) 

Java代码:

private File createImageFile() throws IOException { 
    ContextWrapper cw = new ContextWrapper(getActivity()); 
    // path to /data/data/yourapp/app_data/imageDir 
    File directory = cw.getDir("images", Context.MODE_PRIVATE); 
    if(!directory.exists()) 
     directory.mkdirs(); 
    // Create imageDir 
    File mypath = new File(directory,UUID.randomUUID().toString() +".jpg"); 
    if(!mypath.exists()) 
     mypath.createNewFile(); 
    Uri photoURI = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider",mypath); 
    return mypath; 
} 

private void dispatchTakePictureIntent() throws IOException { 
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    // Create the File where the photo should go 
    File photoFile = null; 
    try { 
     photoFile = createImageFile(); 
    } catch (IOException ex) { 
     // Error occurred while creating the File 
     return; 
    } 
    // Continue only if the File was successfully created 
    if (photoFile != null) { 
     Uri photoURI = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider",createImageFile()); 
     mCurrentPhotoPath = "file:" + photoFile.getAbsolutePath(); 
     cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
     startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 
    } 
}  

我的路径代码:

<files-path name="my_images" path="app_images/"/> 

我的清单代码:

  <provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="com.example.example.provider" 
     android:exported="false" 
     android:grantUriPermissions="true"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/provider_paths"/> 
    </provider> 

对此有何线索?在私有目录cw.getDir("images", Context.MODE_PRIVATE);

+1

描述另一个可用sollution'FileProvider'不能从任意位置,如()''通过创建GETDIR自定义目录服务。可以将文件存储在更常见的位置(如Stanislav所建议的),为[我的StreamProvider](https://github.com/commonsguy/cwac-provider)写一个可以从那里启动的扩展,或者创建自己的文件'ContentProvider'。 – CommonsWare

回答

2

您存储文件选择在docs

相关问题