我的应用访问基本图库应用。
当我选择一张图片时,应用程序就会出现此错误:
E/AndroidRuntime(29852): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/9013 (has extras) }} to activity {com.example.rusk/com.example.rusk.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/9013 from pid=29852, uid=10097 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
我的代码如下:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Intent.createChooser"), SELECT_PICTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Toast.makeText(this, "onActivityResult 실행", Toast.LENGTH_LONG).show();
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
答案 0 :(得分:0)
您应该在清单文件中添加权限,例如
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
正如你的错误所说