无法插入图像。没有权限。尽管提供许可

时间:2017-12-17 13:43:53

标签: java android bitmap permissions mediastore

我尝试使用MediaStore在Android设备的图库中保存位图图像。 这是我的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackagename.myappname">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.mypackagename.myappname.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>
    </provider>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

正如您在清单文件中所看到的,我提供了读写外部存储权限。我还提供了URI权限。我实现了运行时权限。这是我的MainActivity.java:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    testButton = (Button) findViewById(R.id.button2);

    testButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_EXTERNAL_STORAGE);
                }
            } else {
                startSave();
            }
        }
    });

}

public void startSave() {
    imageView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
    String savedImageURL = MediaStore.Images.Media.insertImage(
            getContentResolver(),
            bitmap,
            "EditedImage",
            "MyApp"
    );
}

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if(requestCode == PERMISSION_REQUEST_EXTERNAL_STORAGE){
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
            startSave();
        } else {
            Toast.makeText(this, "Until you grant the permission, we cannot save the photo", Toast.LENGTH_SHORT).show();
        }
    }
}

我还使用以下方式检查了读取权限:

private boolean checkWriteExternalPermission(){
    String permission = "android.permission.WRITE_EXTERNAL_STORAGE";
    int res = getContext().checkCallingOrSelfPermission(permission);
    return (res == PackageManager.PERMISSION_GRANTED);            
}

它也返回false表示我没有获得写入权限。我已在运行时和清单文件中分配了权限。但我仍然遇到了这个错误:

E/MediaStore: Failed to insert image
          java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=6946, uid=10082 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()

我在运行时提供了清单文件和存储读取权限的权限。那么,为什么我一直收到这个错误?有谁有想法吗?我在这里使用Android模拟器进行应用测试。我应该如何使用android的mediaStore功能实现将Bitmap保存到图库的权限?我还通过设置&gt; app&gt; myapp&gt;权限添加了应用程序权限的屏幕截图。 enter image description here

1 个答案:

答案 0 :(得分:3)

testButton的{​​{1}}:

OnClick

阅读更改为 WRITE 。插入图像是一种WRITING操作。您需要申请WRITE权限。