2016-12-06 1 views
-2

こんにちは!セキュリティ例外:READ_EXTERNAL_STORAGE権限

私はエラーを修正する方法をスタックオーバーフローや動画の検索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' 
} 

UPDATE

私はこの追加する問題を修正:

場合(!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; 
+0

をもう一度読んで問題を解決しました。ありがとうございました! – ProgIsMetal

答えて

関連する問題