首页 > 解决方案 > 某些手机​​无法从 android 中的 url(POST) 下载 pdf

问题描述

在此处输入图像描述从流中下载文件(url respunce)我尝试不使用移动设备。但某些移动设备无法使用,文件创建时未写入。我正在更改字节[4096],字节[1024] ...,发生同样的错误请给我任何建议

public void saveToDisk(ResponseBody body, String pdfFileName, String module_type, String test_name) {
    try {
        File sdcard_destinationFile;
        if (module_type.equalsIgnoreCase("ADD")) {

            sdcard_destinationFile = new File(Environment.getExternalStorageDirectory()
                    + "/download/" + pdfFileName + "_" + test_name + ".pdf");
        } else {
            sdcard_destinationFile = new File(Environment.getExternalStorageDirectory()
                    + "/download/" + pdfFileName + ".pdf");
        }


        InputStream is = null;
        OutputStream os = null;

        try {
            Log.e("file",body+"");
            is = body.byteStream();
            os = new FileOutputStream(sdcard_destinationFile);
            byte data[] = new byte[4096];
            int count;
            int progress = 0;
            while ((count = is.read(data)) != -1) {
                os.write(data, 0, count);
                progress += count;
                Log.d(TAG, "Progress: " + progress + "/" + body.contentLength() + " >>>> " + (float) progress / body.contentLength());
            }
            Log.d("aaaaa", "Progress: " + progress + "/" + body.contentLength() + " >>>> " + (float) progress / body.contentLength());
            os.flush();
            Log.e("isis",is +"");
            Log.e("isis",os +"");
            if (is != null) is.close();
            if (os != null) os.close();
            Log.d(TAG, "File saved successfully!");
         showPdf(pdfFileName, module_type, test_name);
           return;
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "Failed to save the file!");
            return;
        } finally {

        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(TAG, "Failed to save the file!");
        return;
    }
}

标签: javaandroid

解决方案


推荐阅读