2014-06-25 35 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"))