权限拒绝:打开提供程序 android.support.v4.content.FileProvider
我在 Android 模拟器中尝试执行应用程序升级时遇到了一些问题.该场景的流程来自一个Activity,我将执行AsyncTask A,打开fragment A,然后在AsyncTask A中,我将检查如果版本升级可用.
I was having some problem when trying to perform an application upgrade in Android emulator. The flow of the scenario is from an Activity, I will execute AsyncTask A which open up fragment A, then inside AsyncTask A, I will check if version upgrade is available.
如果可用且用户从片段 A 中选择了好的",我将继续执行 AsyncTask B 以打开 片段 B给用户的消息说升级正在进行中.在 AsyncTask B doInBackground() 中,我将执行 install() 并在 onPostExecute() 中,我将显示成功信息.
If available and user selected "Okay" from fragment A, I will proceed to AsyncTask B to open up fragment B which show a message to user saying that upgrading is in process. In AsyncTask B doInBackground(), I will execute the install() and in onPostExecute(), I will show successful message.
在我执行安装的 AsyncTask B 中:
In my AsyncTask B where I execute the install:
@Override
protected Boolean doInBackground(Void... params) {
boolean ret= viewmodel.installApk(mActivity);
return ret;
}
在我的视图模型类中:
public boolean installApk(Activity mActivity){
boolean success = false;
String fullPath = scanDirectoryForApk();
System.out.println("INSTALLING APK ......................... ");
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri apkURI = FileProvider.getUriForFile(mActivity, mActivity.getApplicationContext().getPackageName() + ".provider", new File(fullPath));
intent.setDataAndType(apkURI, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mActivity.startActivity(intent);
return true;
}
但是,当我执行上面的代码时,没有显示任何错误消息,并且版本升级也无法正常工作.它基本上只是重新启动意图,根本没有升级.
However, when I execute the code above, no error message was shown and the version upgrade is not working as well. It basically just restart the intent and there is no upgrade at all.
它也不会提示我获得安装新版本的权限.有什么想法吗?
It does not prompt me for the permission to install new version as well. Any ideas?
顺便说一下,我的 android 模拟器没有 root,因此我无法使用su"命令方法.
By the way, my android emulator is not rooted and therefore I could not use the "su" command approach.
谢谢!
编辑
根据@Sagar 的建议,我将上面的代码更改为:
As suggestion by @Sagar, I changed my code above to:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri apkURI = FileProvider.getUriForFile(mActivity, mActivity.getApplicationContext().getPackageName() + ".provider", new File(fullPath));
List<ResolveInfo> resInfoList = mActivity.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
mActivity.grantUriPermission(packageName, apkURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.setDataAndType(apkURI, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mActivity.startActivity(intent);
我从 logcat 收到新的错误消息:
And I am getting new error message from logcat:
Error staging apk from content URI
java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{3883c8 9647:com.google.android.packageinstaller/u0a20} (pid=9647, uid=10020) that is not exported from UID 10085
意图错误消息告诉我解析包时出现问题".
The intent error message is telling me "There was a problem parsing package".
可以尝试合并
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
进入
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);
我遇到了和你一样的问题,这个解决方案救了我自己.
I met the same issue as you, and this solution rescued myself.