Android:无法打开从DownloadManager下载的csv文件
创始人
2024-10-13 21:00:48
0

问题描述: 在Android应用中使用DownloadManager下载了一个csv文件,但是无法打开该文件。

解决方法:

  1. 确保已经添加了适当的权限: 在AndroidManifest.xml文件中添加以下权限:


  1. 确保已经注册了DownloadManager的广播接收器: 在AndroidManifest.xml文件中添加以下代码:

    
        
    

  1. 创建一个广播接收器DownloadCompleteReceiver.java:
public class DownloadCompleteReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        if (downloadId != -1) {
            DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(downloadId);
            Cursor cursor = downloadManager.query(query);
            if (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) {
                        Uri uri = Uri.parse(uriString);
                        String filePath = uri.getPath(); // 获取文件路径
                        if (filePath != null && filePath.endsWith(".csv")) {
                            openCsvFile(context, uri);
                        }
                    }
                }
            }
            cursor.close();
        }
    }

    private void openCsvFile(Context context, Uri uri) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "text/csv");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        try {
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(context, "无法打开csv文件", Toast.LENGTH_SHORT).show();
        }
    }
}
  1. 在需要下载csv文件的地方,使用DownloadManager下载文件:
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(csvFileUrl));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file.csv");
long downloadId = downloadManager.enqueue(request);

这样,当文件下载完成后,会触发DownloadCompleteReceiver中的广播接收器,然后打开下载的csv文件。

相关内容

热门资讯

交流学习经验!wepoker可... 交流学习经验!wepoker可以透视码,wepoker辅助器软件下载,wepoke教程(有挂教程);...
大家学习交流!wepoker辅... 大家学习交流!wepoker辅助器,wepoker免费钻石,普及教程(有挂教程);小薇(透视辅助)致...
每日必看推荐!wepoker有... 每日必看推荐!wepoker有脚本吗,德州透视脚本,科技教程(有挂软件);支持2-10人实时对战,虚...
实测教程!wepoker透视版... 实测教程!wepoker透视版下载,wepoker黑侠辅助器正版下载,可靠教程(有挂透明);支持多人...
重大推荐!wejoker辅助软... 重大推荐!wejoker辅助软件,wepoker手机助手,wpk教程(有挂技巧);最新版2024是一...
一分钟秒懂!wepoker私人... 您好,wepoker私人局俱乐部这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...
分享给玩家!wepoker有辅... 分享给玩家!wepoker有辅助功能吗,wepoker辅助分析器,透牌教程(有挂辅助);建议优先通过...
玩家攻略推荐!德普辅助软件,w... 您好,wepoker破解器激活码这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...
一分钟了解!fishpoker... 一分钟了解!fishpoker透视底牌,wepoker开辅助能查到吗,必胜教程(有挂技巧)1、许多玩...
发现玩家!wejoker黑侠辅... 发现玩家!wejoker黑侠辅助器,wepoker破解游戏盒子,总结教程(有挂软件);亲真的是有正版...