我通过使用DownloadManager.COLUMN_LOCAL_URI
解决了这个问题,而不是使用DownloadManager.COLUMN_LOCAL_FILENAME
DownloadManager.COLUMN_LOCAL_URI
返回包括"file://"在内的文件路径,因此您需要通过使用downloadFilePath = downloadFilePath.replace("file://","");
来排除它。
以下是此问题的一行解决方案:
String downloadFilePath = (c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).replace("file://","");
请查看完整的DownloadManager代码:
DownloadFinishedReceiver.java
public class DownloadFinishedReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action) && intent.getExtras()!=null) {
Bundle extras = intent.getExtras();
DownloadManager.Query q = new DownloadManager.Query();
long downloadId = extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
q.setFilterById(downloadId);
Cursor c = ((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE)).query(q);
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
String downloadFilePath = (c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).replace("file://","");
String downloadTitle = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
c.close();
Log.e("DownloadPath", downloadFilePath);
Log.e("DownloadTitle", downloadTitle);
} else if (status == DownloadManager.STATUS_FAILED) {
removeTempOnFailure(context, downloadId);
}
}
}
}
private void removeTempOnFailure(Context con, long downloadId) {
File cacheFileDir = new File(con.getCacheDir().getAbsolutePath());
for (File f : cacheFileDir.listFiles()) {
if (f.getName().contains(String.valueOf(downloadId))) {
f.delete();
break;
}
}
}
}
在AndroidMenifest.xml文件中注册广播接收器:
<receiver
android:name="com.example.receiver.DownloadFinishedReceiver"
android:exported="true"
android:process=".downloadFinished">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
将以下方法放入您的Activity中并传递适当的参数:
private long downloadFromUrl(String downloadUrl, String downloadTitle, String fileName, boolean hide) {
Uri uri = Uri.parse(downloadUrl);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle(downloadTitle);
if (hide) {
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setVisibleInDownloadsUi(false);
} else
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
return manager != null ? manager.enqueue(request) : 0;
}
如果您在上述方法中传递了hide=true
,那么您需要在AndroidManifest.xml
中添加以下权限。
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>
COLUMN_LOCAL_URI
会提供给你一个Uri
,以便在ContentResolver
上调用openFileDescriptor()
或openInputStream()
来读取已下载的内容。你可以通过在任何Context
(例如某个Service
)上调用getContentResolver()
来获取ContentResolver
。 - CommonsWareCOLUMN_LOCAL_URI
获取的Uri
上的getLastPathSegment()
。 - CommonsWare