授予权限后如何从 onRequestPermissionsResult 中的意图获取数据
How I get data from intent in onRequestPermissionsResult when permission is granted
我想打开存储中的图像。如果该应用程序是第一次运行并且我接受了许可,那么它会崩溃,因为它无法获取要显示的图像的 uri 信息。如何获取有关我选择的图像的 uri 信息?
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
imgLogo.setImageBitmap(null);
Uri orgUri,uriFromPath;
String convertedPath;
//Uri return from external activity
orgUri = data.getData();
//path converted from Uri
convertedPath = getRealPathFromURI(orgUri);
//Uri convert back again from path
uriFromPath = Uri.fromFile(new File(convertedPath));
try {
Bitmap image = MediaStore.Images.Media.getBitmap(getContentResolver(), uriFromPath);
imgLogo.setImageBitmap(image);
} catch (IOException e) {
e.printStackTrace();
}
} else {
Log.e("Permission", "Denied");
}
onRequestPermissionsResult()
回调将仅包含有关您的权限授予结果的数据。对于图像数据,您必须从 onActivityResult()
回调中获取它。
我想打开存储中的图像。如果该应用程序是第一次运行并且我接受了许可,那么它会崩溃,因为它无法获取要显示的图像的 uri 信息。如何获取有关我选择的图像的 uri 信息?
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
imgLogo.setImageBitmap(null);
Uri orgUri,uriFromPath;
String convertedPath;
//Uri return from external activity
orgUri = data.getData();
//path converted from Uri
convertedPath = getRealPathFromURI(orgUri);
//Uri convert back again from path
uriFromPath = Uri.fromFile(new File(convertedPath));
try {
Bitmap image = MediaStore.Images.Media.getBitmap(getContentResolver(), uriFromPath);
imgLogo.setImageBitmap(image);
} catch (IOException e) {
e.printStackTrace();
}
} else {
Log.e("Permission", "Denied");
}
onRequestPermissionsResult()
回调将仅包含有关您的权限授予结果的数据。对于图像数据,您必须从 onActivityResult()
回调中获取它。