我想下载.apk文件并安装它。当我没有使用FileProvider时,一切进展顺利,但是当我使用FileProvider从文件创建uri时,我得到了IllegalArgumentException:不是文件URI:content://pl.rasztabiga.klasa1a.provider/external_storage_root/klasa1a。 apk在线
final long downloadId = manager.enqueue(request);
我尝试了stackoverflow中的所有内容,但没有任何帮助。这是我的代码:
File file = new File(Environment.getExternalStorageDirectory(), "klasa1a.apk");
final Uri uri = FileProvider.getUriForFile(MainActivity.this, getApplicationContext().getPackageName() + ".provider", file);
//Delete update file if exists
//File file = new File(destination);
if (file.exists())
file.delete();
//get url of app on server
String url = "http://rasztabiga.ct8.pl/klasa1a.apk";
//set downloadmanager
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Downloading new version");
request.setTitle(MainActivity.this.getString(R.string.app_name));
//set destination
request.setDestinationUri(uri);
// get download service and enqueue file
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
//set BroadcastReceiver to install app when .apk is downloaded
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.setDataAndType(uri,
manager.getMimeTypeForDownloadedFile(downloadId));
startActivity(install);
unregisterReceiver(this);
finish();
}
};
//register receiver for when .apk download is compete
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
答案 0 :(得分:2)
ACTION_VIEW
和ACTION_INSTALL_PACKAGE
仅支持Android 7.0中的content
计划。在此之前,您别无选择,只能使用file
。所以,改变:
final Uri uri = FileProvider.getUriForFile(MainActivity.this, getApplicationContext().getPackageName() + ".provider", file);
为:
final Uri uri = (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N) ?
FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider", file) :
Uri.fromFile(file);
答案 1 :(得分:2)
问题出在DownloadManager中。它无法解析uri" content://",仅作为" file://"所以自sdk24以来,我们无法使用它。使用常见的IOStreams和HttpURLConnection一切正常。感谢@CommonsWare向我展示了他的项目。 这就是现在的样子:
File file = new File(Environment.getExternalStorageDirectory(), "klasa1a.apk");
final Uri uri = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) ?
FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider", file) :
Uri.fromFile(file);
//Delete update file if exists
//File file = new File(destination);
if (file.exists())
//file.delete() - test this, I think sometimes it doesnt work
file.delete();
//get url of app on server
String url = "http://rasztabiga.ct8.pl/klasa1a.apk";
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL sUrl = new URL(url);
connection = (HttpURLConnection) sUrl.openConnection();
connection.connect();
// download the file
input = connection.getInputStream();
output = new FileOutputStream(file);
byte data[] = new byte[4096];
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled()) {
input.close();
return null;
}
output.write(data, 0, count);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE)
.setData(uri)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(install);
return null;
}
答案 2 :(得分:1)
对于仍想使用DownloadManager
并发现CommonsWare的答案不能解决问题的用户,您需要为APK文件添加一个例外情况
final Uri uri = (!file.getAbsolutePath().endsWith(".apk") && Build.VERSION.SDK_INT>=Build.VERSION_CODES.N) ?
FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider", file) :
Uri.fromFile(file);
request.setDestinationUri(uri);
哪个是
的缩写if (!file.getAbsolutePath().endsWith(".apk") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
request.setDestinationUri(FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider", download));
} else {
request.setDestinationUri(Uri.fromFile(file));
}
这可能更容易理解。
简短的回答是,应该使用Uri.fromFile
处理apk文件,但这不必以更改整个过程为代价。
不过,如果您想更周全,还可以将IllegalArgumentException
捕获到Not a file URI
并使用它来尝试Uri.fromFile