我創建了一個應用程序,它從模擬器中的圖像文件夾中提取圖像,並在我的應用程序佈局中顯示所選圖像。
當我將應用程序轉換爲即時應用程序時,從圖像文件夾中拾取圖像的相同代碼會引發異常。我使用了運行時權限READ_EXTERNAL_STORAGE。
下面是我的代碼:
無法從Android即時應用程序啓動畫廊
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=*/* }
**
以下是代碼爲r不及時的權限:
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;
}
}
任何人都可以幫助我解決這個問題。
在此先感謝。
那麼你是如何處理它的? – Kenji