使用 FileProvider 时未找到 Activity 来处理 Intent
No Activity found to handle Intent when using FileProvider
在我的应用程序中,我有一个自定义的自动下载和安装 APK,它的工作方式如下
// auto register for the complete download
activity.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
// Download the file through DownloadManager
String destination = Environment.getExternalStorageDirectory() + "/";
String fileName = "myfile.apk";
destination += fileName;
final Uri uri = Uri.parse("file://" + destination);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
request.setDescription("description");
request.setTitle("title");
request.setDestinationUri(uri);
final DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Intent install = new Intent(Intent.ACTION_VIEW);
// BEFORE working doing this
//install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//install.setDataAndType(uri,
// manager.getMimeTypeForDownloadedFile(downloadId));
// Using file provider it doesnt work
Uri apkUri = FileProvider.getUriForFile(AutoUpdate.this,
"com.myapp", file);
install.setDataAndType(apkUri,manager.getMimeTypeForDownloadedFile(downloadId));
activity.startActivity(install);
activity.unregisterReceiver(this);
}
};
Android 清单:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
Provider_path(抱歉,由于某些原因所以删除了路径标签)
external-path name="myfolder" path="."/>
当文件下载完成时调用 onComplete 但 activiy 没有启动:
No Activity found to handle Intent { act=android.intent.action.VIEW
dat=content://com.myapp/myfolder/myfile.apk
typ=application/vnd.android.package-archive flg=0x4000000 }
当使用普通的 file:// 时它确实有效
我在使用文件提供程序时是否遗漏了什么? activity 是否因为找不到文件而没有启动?
我需要额外的许可吗? (目前我有互联网,在外部存储上读写)
软件包安装程序仅支持从 Android 7.0 开始的 content
方案。在此之前——尽管有相反的文档——软件包安装程序只支持 file
方案。
您需要根据您是否 Android 7.0+ 上的 运行 不同地设置 Intent
上的 Uri
,例如通过分支Build.VERSION.SDK_INT
.
找不到您的提供商,因为
android:enabled="true"
您的清单中缺少。
但是再看CW的其他回答也不行了
通用软件是对的,
对于那些寻找代码的人:
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
downloadButton.setEnabled(true);
downloadButton.setText("Terminado");
progressbar.setVisibility(View.GONE);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Uri apkURI = FileProvider.getUriForFile(
self,
self.getApplicationContext()
.getPackageName() + ".provider", file);
install.setDataAndType(apkURI,
manager.getMimeTypeForDownloadedFile(downloadId));
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(install);
} else{
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = "surveyevent.apk";
destination += fileName;
Uri uri = Uri.parse("file://" + destination);
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(uri,
manager.getMimeTypeForDownloadedFile(downloadId));
startActivity(install);
}
unregisterReceiver(this);
finish();
}
};
在我的应用程序中,我有一个自定义的自动下载和安装 APK,它的工作方式如下
// auto register for the complete download
activity.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
// Download the file through DownloadManager
String destination = Environment.getExternalStorageDirectory() + "/";
String fileName = "myfile.apk";
destination += fileName;
final Uri uri = Uri.parse("file://" + destination);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
request.setDescription("description");
request.setTitle("title");
request.setDestinationUri(uri);
final DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Intent install = new Intent(Intent.ACTION_VIEW);
// BEFORE working doing this
//install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//install.setDataAndType(uri,
// manager.getMimeTypeForDownloadedFile(downloadId));
// Using file provider it doesnt work
Uri apkUri = FileProvider.getUriForFile(AutoUpdate.this,
"com.myapp", file);
install.setDataAndType(apkUri,manager.getMimeTypeForDownloadedFile(downloadId));
activity.startActivity(install);
activity.unregisterReceiver(this);
}
};
Android 清单:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
Provider_path(抱歉,由于某些原因所以删除了路径标签)
external-path name="myfolder" path="."/>
当文件下载完成时调用 onComplete 但 activiy 没有启动:
No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.myapp/myfolder/myfile.apk typ=application/vnd.android.package-archive flg=0x4000000 }
当使用普通的 file:// 时它确实有效
我在使用文件提供程序时是否遗漏了什么? activity 是否因为找不到文件而没有启动? 我需要额外的许可吗? (目前我有互联网,在外部存储上读写)
软件包安装程序仅支持从 Android 7.0 开始的 content
方案。在此之前——尽管有相反的文档——软件包安装程序只支持 file
方案。
您需要根据您是否 Android 7.0+ 上的 运行 不同地设置 Intent
上的 Uri
,例如通过分支Build.VERSION.SDK_INT
.
找不到您的提供商,因为
android:enabled="true"
您的清单中缺少。
但是再看CW的其他回答也不行了
通用软件是对的, 对于那些寻找代码的人:
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
downloadButton.setEnabled(true);
downloadButton.setText("Terminado");
progressbar.setVisibility(View.GONE);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Uri apkURI = FileProvider.getUriForFile(
self,
self.getApplicationContext()
.getPackageName() + ".provider", file);
install.setDataAndType(apkURI,
manager.getMimeTypeForDownloadedFile(downloadId));
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(install);
} else{
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = "surveyevent.apk";
destination += fileName;
Uri uri = Uri.parse("file://" + destination);
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(uri,
manager.getMimeTypeForDownloadedFile(downloadId));
startActivity(install);
}
unregisterReceiver(this);
finish();
}
};