2017-08-16 44 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

相關問題