我需要在我的intent过滤器中访问存储,以便在Android 6.0应用程序中获取pdf文件或图像。
[IntentFilter(new[] { Intent.ActionOpenDocument, Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataMimeType = "application/pdf")]
[IntentFilter(new[] { Intent.ActionOpenDocument, Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataMimeType = "image/jpeg")]
[IntentFilter(new[] { Intent.ActionOpenDocument, Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataMimeType = "image/png")]
但是,它需要来自app的用户授予存储权限。
问题是这种情况是否有适当的POC,我可以阻止意图,显示警报,然后在完成基于授予/撤销许可的意图后完成?
目前我假设我只能显示一些没有权限的吐司,并轻轻地要求用户进入应用并手动启用权限(从某些自定义应用设置或其他任何设置)。
答案 0 :(得分:0)
private static final int REQUEST_RUNTIME_PERMISSION = 1;
void checkPremission() {
//Desired permission you want
final String permission = Manifest.permission.CAMERA;
// if in fragment use getActivity()
if (ContextCompat.checkSelfPermission(ActivityName.this, permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(ActivityName.this, permission)) {
} else {
ActivityCompat.requestPermissions(ActivityName.this, new String[]{permission}, REQUEST_CAMERA_PERMISSION);
}
} else {
// you have permission go ahead
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_RUNTIME_PERMISSION:
final int numOfRequest = grantResults.length;
final boolean isGranted = numOfRequest == 1
&& PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
if (isGranted) {
// you have permission go ahead
}else{
// you dont have permission show toast
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
答案 1 :(得分:0)
private void requestPermission() {
int writePermission = ContextCompat.checkSelfPermission(
MyActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (writePermission != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
// if (ActivityCompat.shouldShowRequestPermissionRationale(
// MyActivity.this,
// Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//
// // Show an expanation to the user *asynchronously* -- don't
// // block
// // this thread waiting for the user's response! After the user
// // sees the explanation, try again to request the permission.
//
// } else {
// No explanation needed, we can request the permission.
ActivityCompat
.requestPermissions(
MyActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_WRITE_TO_EXTERNAL_STORAGE);
// MY_PERMISSIONS_WRITE_TO_EXTERNAL_STORAGE is an
// app-defined int constant. The callback method gets the
// result of the request.
// }
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_WRITE_TO_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// permission-related task you need to do.
} else {
MyActivity.this.finish();
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
我在这里使用了WRITE_EXTERNAL_STORAGE权限,因为它授予了读取和写入两者的权限。
调用您需要的requestPermission()
方法,然后从您的设备获取pdf或图像。
希望这有帮助。
答案 2 :(得分:0)
感谢您的回答,但也许我忘记在问题中提及它,但我标记了它 - 我正在寻找Xamarin技术的答案。
时间过去了,但是不久前,我们找到了解决方案。它更像是一段代码:
self.kwargs.get('pk', None)