文件下载提交
This commit is contained in:
parent
4964c7e207
commit
e091197527
|
|
@ -215,10 +215,23 @@ public class FirstActivity extends BaseActivity {
|
|||
// || url.endsWith(".xlsx")
|
||||
// || url.contains("/api/file/");
|
||||
}
|
||||
private long currentDownloadId = -1; // 保存当前下载任务ID
|
||||
|
||||
private void downloadFile(String url) {
|
||||
try {
|
||||
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
|
||||
if (TextUtils.isEmpty(url)) {
|
||||
Toast.makeText(this, "下载地址为空", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.startsWith("blob:") || url.startsWith("data:")) {
|
||||
Toast.makeText(this, "无法直接下载 Blob 文件,请检查前端下载逻辑", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
Uri uri = Uri.parse(url);
|
||||
DownloadManager.Request request = new DownloadManager.Request(uri);
|
||||
|
||||
String fileName = URLUtil.guessFileName(url, null, null);
|
||||
request.setTitle(fileName);
|
||||
request.setDescription("正在下载文件...");
|
||||
|
|
@ -227,10 +240,64 @@ public class FirstActivity extends BaseActivity {
|
|||
request.allowScanningByMediaScanner();
|
||||
request.addRequestHeader("User-Agent", System.getProperty("http.agent"));
|
||||
|
||||
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
|
||||
dm.enqueue(request);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
request.setRequiresCharging(false);
|
||||
request.setAllowedOverMetered(true);
|
||||
request.setAllowedOverRoaming(true);
|
||||
}
|
||||
|
||||
String mimeType = getMimeTypeForUrl(url);
|
||||
request.setMimeType(!TextUtils.isEmpty(mimeType) ? mimeType : "application/octet-stream");
|
||||
|
||||
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
|
||||
if (dm != null) {
|
||||
currentDownloadId = dm.enqueue(request);
|
||||
Toast.makeText(this, "文件已开始下载,请等待", Toast.LENGTH_SHORT).show();
|
||||
|
||||
// ✅ 注册广播监听下载完成事件
|
||||
registerReceiver(new android.content.BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, android.content.Intent intent) {
|
||||
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
|
||||
if (id == currentDownloadId) {
|
||||
DownloadManager.Query query = new DownloadManager.Query();
|
||||
query.setFilterById(id);
|
||||
android.database.Cursor cursor = dm.query(query);
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
|
||||
if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(columnIndex)) {
|
||||
String uriString = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
|
||||
if (uriString != null) {
|
||||
try {
|
||||
Uri fileUri = Uri.parse(uriString);
|
||||
String type = getMimeTypeForUrl(fileUri.toString());
|
||||
|
||||
android.content.Intent openIntent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
|
||||
openIntent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK | android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
openIntent.setDataAndType(fileUri, type);
|
||||
|
||||
// ✅ 检查系统是否有能打开的 App
|
||||
if (openIntent.resolveActivity(context.getPackageManager()) != null) {
|
||||
context.startActivity(openIntent);
|
||||
} else {
|
||||
Toast.makeText(context, "下载完成,请前往“下载”文件夹手动打开该文件", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Toast.makeText(context, "下载完成,但无法打开文件: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
// ✅ 只监听一次下载任务
|
||||
unregisterReceiver(this);
|
||||
}
|
||||
}
|
||||
}, new android.content.IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
||||
} else {
|
||||
Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
Toast.makeText(this, "文件已开始下载,完成后可在通知栏查看", Toast.LENGTH_SHORT).show();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Toast.makeText(this, "下载失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
|
|
|
|||
Loading…
Reference in New Issue