我创建了一个应用程序,它从模拟器中的图像文件夹中提取图像,并在我的应用程序布局中显示选定的图像。当我将我的应用程序转换为即时应用程序时,从图像文件夹中提取图像的相同代码会引发异常。我使用了运行时权限 READ_EXTERNAL_STORAGE。
我的代码:
private static final int PICK_IMAGE_REQUEST = 234;
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
抛出异常:
java.lang.SecurityException: Not allowed to start activity Intent {
act=android.intent.action.GET_CONTENT typ=*/* }
运行时权限代码:
private static final int REQUEST_WRITE_STORAGE = 112;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public boolean checkPermission()
{
int currentAPIVersion = Build.VERSION.SDK_INT;
if(currentAPIVersion>=android.os.Build.VERSION_CODES.M)
{
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
Toast.makeText(MainActivity.this,"Permission Denied...",Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_WRITE_STORAGE);
return true;
}
return false;
} else {
return true;
}
} else {
return true;
}
}
谁能帮我解决这个问题?