DownloadManager:no handler found for type application/epub+zip

若你通过DownloadManager来来download一些比如epub等这样格式的file,你会遇到no handler found for type application/epub+zip这样的error message.这是因为DownloadManger只允许download能处理的file而那些file没有对应的Activity处理的就会选择不download.具体的代码:
package com.android.providers.downloads;
Helpers.java

 /**
* Creates a filename (where the file should be saved) from a uri.
*/
public static DownloadFileInfo generateSaveFile(
Context context,
String url,
String hint,
String contentDisposition,
String contentLocation,
String mimeType,
int destination,
int contentLength) throws FileNotFoundException {

/*
* Don't download files that we won't be able to handle
*/
if (destination == Downloads.DESTINATION_EXTERNAL
|| destination == Downloads.DESTINATION_CACHE_PARTITION_PURGEABLE) {
if (mimeType == null) {
if (Config.LOGD) {
Log.d(Constants.TAG, "external download with no mime type not allowed");
}
return new DownloadFileInfo(null, null, Downloads.STATUS_NOT_ACCEPTABLE);
}
if (!DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING.equalsIgnoreCase(mimeType)) {
// Check to see if we are allowed to download this file. Only files
// that can be handled by the platform can be downloaded.
// special case DRM files, which we should always allow downloading.
Intent intent = new Intent(Intent.ACTION_VIEW);

// We can provide data as either content: or file: URIs,
// so allow both. (I think it would be nice if we just did
// everything as content: URIs)
// Actually, right now the download manager's UId restrictions
// prevent use from using content: so it's got to be file: or
// nothing

PackageManager pm = context.getPackageManager();
intent.setDataAndType(Uri.fromParts("file", "", null), mimeType);
ResolveInfo ri = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
//Log.i(Constants.TAG, "*** FILENAME QUERY " + intent + ": " + list);

if (ri == null) {
if (Config.LOGD) {
Log.d(Constants.TAG, "no handler found for type " + mimeType);
}
return new DownloadFileInfo(null, null, Downloads.STATUS_NOT_ACCEPTABLE);
}
}
}
String filename = chooseFilename(
url, hint, contentDisposition, contentLocation, destination);

// Split filename between base and extension
// Add an extension if filename does not have one
String extension = null;
int dotIndex = filename.indexOf('.');
if (dotIndex < 0) {
extension = chooseExtensionFromMimeType(mimeType, true);
} else {
extension = chooseExtensionFromFilename(
mimeType, destination, filename, dotIndex);
filename = filename.substring(0, dotIndex);
}


中的
PackageManager pm = context.getPackageManager();
intent.setDataAndType(Uri.fromParts("file", "", null), mimeType);
ResolveInfo ri = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
//Log.i(Constants.TAG, "*** FILENAME QUERY " + intent + ": " + list);

if (ri == null) {
if (Config.LOGD) {
Log.d(Constants.TAG, "no handler found for type " + mimeType);
}
return new DownloadFileInfo(null, null, Downloads.STATUS_NOT_ACCEPTABLE);
就是对这个的处理。
若你要使它能download你想要download的任何类型的file,你可以将上面这段代码去掉。也就是对DownloadManager进行修改。或许你不想修改DownlodManager进行修改,那么你可以增加一个Activity来处理你的特定类型的file。这样DownloadManager就能找到这个Activtiy,就能下载了。比如:
<activity android:name=".ViewDownload" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/epub+zip" />
</intent-filter>

</activity>

这样就能下载epub这样file了。
具体的你可以参考这篇文章:
[url]http://a.77mi.com/Thread/view/id-14016[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值