我的webView应用程序在用户点击文件上传时正在请求权限
@Override
public boolean onShowFileChooser(WebView view, ValueCallback < Uri[] > valueCallback, WebChromeClient.FileChooserParams fileChooserParams) {
Context context = getApplicationContext();
Log.d("LOG", "Triggered 1");
if (fileChooserCallback != null) {
fileChooserCallback.onReceiveValue(null);
}
fileChooserCallback = valueCallback;
String[] permissions = new String[] {
android.Manifest.permission.CAMERA,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.READ_EXTERNAL_STORAGE
};
Log.d("LOG", "Triggered 2");
if (!hasPermissions(context, permissions)) {
ActivityCompat.requestPermissions(MainActivity.this, permissions, PERMISSION_CAMERA_WITH_STORAGE);
} else {
startPictureSelectionActivity();
}
return true;
}
private boolean hasPermissions(Context context, String...permissions) {
for (String permission: permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
if (requestCode == PERMISSION_CAMERA_WITH_STORAGE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startPictureSelectionActivity();
}
} else if (requestCode == PERMISSION_FINE_LOCATION) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
locationCallback.invoke(locationOrigin, true, true);
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
如果用户同意授予请求的权限,则上传工作正常,但如果他不同意,则应用程序对上传点击不再反应(只是什么也没有发生,日志为空),直到用户重新启动应用程序为止。有什么问题吗?
我需要第二个点击应用上的内容才能再次请求权限,因此直到用户同意所请求的权限。例如,如果用户拒绝了所有权限,则Google Chrome和Mozilla Firefox都会打开最近的文件,并且在下次尝试上传文件时再次询问权限。
答案 0 :(得分:0)
覆盖onRequestPermissionsResult()
,检查是否已授予许可,然后在其中调用startPictureSelectionActivity();
。
答案 1 :(得分:0)
添加了拒绝权限的意图,现在可以正常工作,与Mozilla Firefox的工作相同。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
if (requestCode == PERMISSION_CAMERA_WITH_STORAGE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startPictureSelectionActivity();
} else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, FILECHOOSER_RESULT_CODE);
}
} else if (requestCode == PERMISSION_FINE_LOCATION) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
locationCallback.invoke(locationOrigin, true, true);
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}