I'm using DownloadManager
to download a new version of the APK and install it (can't use Google Play).
From other questions I learned that starting from Android 7 I
need to use FileProvider
to start the apk, but I'm getting an error:
Failed to find configured root that contains
The code for downloading and receiver:
private void startDownload3(String url) {
final DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle(getString(R.string.downloading_update_title));
String filename = ((int)(Math.random()*100))+getString(R.string.update_file_name_base);
Logger.e("downloading file = "+filename );
request.setVisibleInDownloadsUi(true);
request.setDestinationInExternalPublicDir("/Download", filename);
saveFileName(filename);
final long enqueue = dm.enqueue(request);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
openFile();
}
}
};
registerReceiver(receiver,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
private void openFile() {
File[] files = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).listFiles();
Logger.e("folder Size: "+ files.length);
String fileName = getFileName(this);
File realFile=null;
for (int i = 0; i < files.length; i++)
{ //for testing purposes
if (fileName.equals(files[i].getName()))
realFile=files[i];
Logger.e("file names : "+ files[i].getName());
}
if(realFile!=null) {
Intent install;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //android 7+
Uri apkUri = FileProvider.getUriForFile(MyFirebaseMessagingService.this, "mmn.policy.Visitors2.fileprovider", realFile);
install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
install.setData(apkUri);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(install);
} else { //todo TEST THIS android 6-
Uri apkUri = Uri.fromFile(realFile);
install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(apkUri, "application/vnd.android.package-archive");
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(install);
}
}
}
And the filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="Download" path="Download/" />
</paths>
And in Manifest
<provider
android:name=".GenericFileProvider"
android:authorities="com.example.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
Now I see the file in the test I made but the FileProvider
fails. Is there any way to get the FileProvider
to point to the same place as setDestinationInExternalPublicDir
?
Full logcat:
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Download/54test.apk
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:712)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:401)
at com.example.MyFirebaseMessagingService.openFile(MyFirebaseMessagingService.java:310)
at com.example.MyFirebaseMessagingService.access$000(MyFirebaseMessagingService.java:49)
at com.example.MyFirebaseMessagingService$3.onReceive(MyFirebaseMessagingService.java:276)