【问题标题】:Read an inputstream and download using DownloadManager使用 DownloadManager 读取输入流并下载
【发布时间】:2014-07-30 23:37:50
【问题描述】:

我正在尝试下载文件并将其保存到 SD 卡。文件地址如下

http://test.com/net/Webexecute.aspx?fileId=120

此 url 提供数据流。我有以下选项来读取输入流。

  • 使用通用输入和输出流(没有连接失败的处理程序 过)

  • 下载管理器

  • 使用 HttpUrlConnection(可能的超时机会)

我已经使用选项 a 完成了下载。但是没有用于连接故障转移的处理程序。所以我决定选择b选项

DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse("http://test.com/net/Webexecute.aspx?fileId="+ fileId));
request.setMimeType("application/pdf");
request.setDescription("fileDownload");
request.setTitle(fileName);
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
dm.enqueue(request);

它正在下载文件。但是,该文件似乎已损坏。

在进行研究时,我从未发现 DownloadManager 被用于获取输入流并将其保存到文件中。我有什么不足吗?

【问题讨论】:

    标签: android android-download-manager


    【解决方案1】:

    请更改您的代码以下载文件。

    protected Void downLoadFile(String fileURL) {
        int count;
    
        try {
            URL url = new URL(fileURL);
            URLConnection conexion = url.openConnection();
            conexion.connect();
            int lenghtOfFile = conexion.getContentLength();
            InputStream is = url.openStream();
    
            File testDirectory = new File(Environment.getExternalStorageDirectory() + "/Download");
            if (!testDirectory.exists()) {
                testDirectory.mkdir();
            }
    
            FileOutputStream fos = new FileOutputStream(testDirectory + "/filename.txt");
    
            byte data[] = new byte[1024];
            long total = 0;
            int progress = 0;
    
            while ((count = is.read(data)) != -1) {
                total += count;
                int progress_temp = (int) total * 100 / lenghtOfFile;
    
                fos.write(data, 0, count);
    
            }
            is.close();
            fos.close();
    
            readStringFromFile(testDirectory);
    
        } catch (Exception e) {
            Log.e("ERROR DOWNLOADING", "Unable to download" + e.getMessage());
            e.printStackTrace();
        }
        
        return null;
    }
    

    Below 方法用于从文件中读取字符串。

        public String readStringFromFile(File file) {
            String response = "";
    
            try {
                FileInputStream fileInputStream = new FileInputStream(file + "/filename.txt");
                StringBuilder builder = new StringBuilder();
                int ch;
                while ((ch = fileInputStream.read()) != -1) {
                    builder.append((char) ch);
                }
                response = builder.toString();
    
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return response;
        }
    

    【讨论】:

    • 感谢您的意见!是的,这会奏效。我有工作流阅读器。但是,此方法没有用于连接故障转移的处理程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 2017-01-09
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多