Android- 保存文件(输入和输出流)在 Pre-lollipop 设备中抛出异常(打开失败:EINVAL(无效参数))

Android- Saving File (Input & Output Stream) throws Exception (open failed: EINVAL (Invalid argument)) in Pre-lollipop devices

Android- 保存文件(输入和输出流)在 Pre-lollipop 设备中抛出异常(打开失败:EINVAL(无效参数))

我正在从 URL (www.xyz.in/file/9) 下载文件并保存在 SD 卡中。

文件夹创建代码:

try {
    //String folder_name = "NewFolder";
    File f = new File(Environment.getExternalStorageDirectory(), "Venky");
    if (!f.exists()) {
        f.mkdirs();
    }
} catch (Exception e) {
    Log.v("MTV", " createFileDirectory exception" + e);
}

下面我发布了一个(下载和保存文件)代码, 在android lollipop 中,可以正常工作 下载文件并将其保存在SD 卡中。

它不适用于 Pre-lolipop 设备。它抛出异常。

protected String doInBackground(String... f_url) {
            int count;
            try {
                URL url = new URL(f_url[0]);
                URLConnection conection = url.openConnection();
                conection.connect();
                // this will be useful so that you can show a tipical 0-100% progress bar
                int lenghtOfFile = conection.getContentLength();

                    String depo = conection.getHeaderField("Content-Disposition");
                    String depoSplit[] = depo.split("filename=");
                    filename = depoSplit[1].replace("filename=", "").replace("\"", "").trim();

                filename=filename.trim();
                String Filetype = conection.getContentType();
                Log.v("fileName", " " + filename + " FileLength " + lenghtOfFile + " Filetype " + Filetype);

                InputStream input = new BufferedInputStream(url.openStream(), 8192);

                OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().toString() + "/Venky/" + filename);
                Log.v("", "" + Environment.getExternalStorageDirectory().toString() + "/" + R.string.app_name + "/" + filename);
                byte data[] = new byte[1024];
                long total = 0;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    // After this onProgressUpdate will be called
                    publishProgress("" + (int) ((total * 100) / lenghtOfFile));
                    // writing data to file
                    output.write(data, 0, count);
                }
                // flushing output
                output.flush();
                // closing streams
                output.close();
                input.close();
                DownloadSucess = true;
            } catch (Exception e) {
                DownloadSucess = false;
                Log.e("Error: ","Download Exception "+ e);
            }

            return null;
        }

Logcat(例外):

>E/Error:: Download Exception java.io.FileNotFoundException: /storage/sdcard0/Venky/FileName : smile-please.png: open failed: EINVAL (Invalid argument)

注意:在清单中,使用了 WRITE_EXTERNAL_STORAGEREAD_EXTERNAL_STORAGE 权限。

你的变量 filename 的值是 FileName : smile-please.png 而不是 - 你认为的 - smile-please.png.

使用前只记录文件名的内容。

我想你应该先尝试创建那个目录和文件,然后检查它们是否存在。

String path = Environment.getExternalStorageDirectory().toString() + "/Venky/";

File dir = new File(path);

boolean created = dir.mkdir();

if (created) {
    File fp = new File(path + filename);}