gpt4 book ai didi

Android- 保存文件(输入和输出流)抛出异常(打开失败 : EINVAL (Invalid argument)) in Pre-lollipop devices

转载 作者:搜寻专家 更新时间:2023-11-01 07:51:04 29 4
gpt4 key购买 nike

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)

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

最佳答案

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

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

关于Android- 保存文件(输入和输出流)抛出异常(打开失败 : EINVAL (Invalid argument)) in Pre-lollipop devices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34943195/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com