2014-06-25 60 views
0

我正在嘗試將裁剪圖像寫入內部存儲,但得到Permision denied意圖寫入內部存儲

這裏的意圖:

Intent photoPickerIntent = new Intent(
         Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       photoPickerIntent.setType("image/*"); 
       photoPickerIntent.putExtra("crop", "true"); 
       photoPickerIntent.putExtra("aspectX", 1); 
       photoPickerIntent.putExtra("aspectY", 1); 
       photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getBackgroundUri()); 
       photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString()); 

       startActivityForResult(photoPickerIntent, ActivityCrop); 


這裏的URI文件:

06-25 21:27:46.644: W/GenPrefsFragment(10251): getBackgroundUri(): file:///data/data/de.dawnkeeper.androidwall/background.jpg 

,這裏是錯誤:

06-25 21:17:30.238: W/CropImage(9001): cannot write output 
06-25 21:17:30.238: W/CropImage(9001): java.io.FileNotFoundException: Permission denied 
06-25 21:17:30.238: W/CropImage(9001): at android.os.Parcel.openFileDescriptor(Native Method) 
06-25 21:17:30.238: W/CropImage(9001): at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:117) 



據因爲我知道沒有任何許可需要訪問內部存儲。
我錯過了什麼?
有沒有更好的方法來解決這個問題?

編輯

我同時找到了解決辦法。如果我在意向啓動之前創建文件,並使用file.setWritable(true,false),那麼這個意圖的工作原理就是。

我仍然接受Hans Kratz的答案,因爲它是一個更清潔的解決方案。

+0

你已經添加到manifest.xml中 <用途的許可安卓:名稱=「android.permission.WRITE_EXTERNAL_STORAGE」 />清單檔案中的? – bhowden

+0

是,權限在清單中。 – Dawnkeeper

+0

是否在元素之外? – bhowden

回答

1

由於Media Store Content Provider無法寫入應用程序的專用存儲空間,因此無法使用內部存儲。讓getBackgroundUri()返回到外部存儲的路徑,例如像Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "test.jpg"))