SecurityException:READ_EXTERNAL_STORAGE权限

时间:2016-12-06 20:59:09

标签: android permissions imageview

大家好!

我花了4个小时搜索堆栈溢出或视频如何修复错误: 引起:java.lang.SecurityException:权限拒绝[...] 需要android.permission.READ_EXTERNAL_STORAGE或grantUriPermission()

 Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/35 from pid=15358, uid=10074 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

我在我的应用程序中尝试显示图库中的图片。 这是清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".AddCountry" android:label="Add Country"></activity>
</application>

我尝试显示图片的活动

public class AddCountry extends AppCompatActivity implements Constant{

private ImageView imageView;
private Button btnUploadPhotos;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_country);
    initComponents();
}

private void initComponents() {
    imageView=(ImageView) findViewById(R.id.imgView_add_country_activity_uploadphotos);
    btnUploadPhotos=(Button)findViewById(R.id.btn_add_country_activity_uploadphotos);
    btnUploadPhotos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openGallery();
        }


    });

}

private void openGallery() {
    Intent gallery=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery,ADD_COUNTRY_REQUEST_PICK_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK&&requestCode==ADD_COUNTRY_REQUEST_PICK_IMAGE)
    {
        Uri imageUri=data.getData();
        String[] filePathColumn={MediaStore.Images.Media.DATA};
        Cursor cursor=getContentResolver().query(imageUri,filePathColumn,null,null,null);
        cursor.moveToFirst();

        int columnIndex=cursor.getColumnIndex(filePathColumn[0]);
        String picturePath=cursor.getString(columnIndex);
        cursor.close();

        Bitmap bitmap=BitmapFactory.decodeFile(picturePath);
        Drawable drawable=new BitmapDrawable(bitmap);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}

首先我尝试了onActivityResult:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK&&requestCode==ADD_COUNTRY_REQUEST_PICK_IMAGE)
    {
        Uri imageUri=data.getData();
        imageView.setImageURI(imageUri);
    }
}

我没有错误,但是当我选择图像时没有任何反应。

应用包:

android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
    applicationId "com.example.rares.proiect"
    minSdkVersion 16
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'
}

更新

我解决了添加此问题的问题:

if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)         != PackageManager.PERMISSION_GRANTED){

// Should we show an explanation?
if (shouldShowRequestPermissionRationale(
        Manifest.permission.READ_EXTERNAL_STORAGE)) {
    // Explain to the user why we need to read the contacts
}

requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
        MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

// MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
// app-defined int constant

return;

1 个答案:

答案 0 :(得分:0)

由于您的目标是sdk&gt; = 23,因此您需要查看runtime permissions