java.io.IOException异常:打开失败:EINVAL(无效参数)保存图像到外部存储时 [英] java.io.ioexception: open failed: einval (Invalid argument) when saving a image to external storage
问题描述
这是我的code:
私人布尔writeToSD(位图BM,字符串URL){
如果(canIWriteOnSD()){
文件SD = Environment.getExternalStorageDirectory();
文件DEST =新的文件(SD,MoveInBlue /);
尝试 {
URL = urlCleaner(URL);
如果(!dest.exists()){
dest.mkdir();
}
档案文件=新的文件(DEST,网址+巴纽);
如果(!file.exists()){
file.createNewFile();
}
FileOutputStream中出=新的FileOutputStream(文件);
bm.com preSS(Bitmap.Com pressFormat.PNG,90,出);
了out.flush();
out.close();
返回true;
}赶上(例外五){
e.printStackTrace();
// 没做什么
}
}
返回false;
}
问题解决了:
urlCleaner现在返回url.substring(url.lastIndexOf(?)+ 1),一切都按计划进行。
该异常实际上是扔在 file.createNewFile();
,我真的不知道为什么
非常感谢你。
(urlCleaner只是删除了HTTP:// ...从URL和叶PHP标签)
这里的LogCat中:
07-09 13:57:13.479:W / System.err的(5715):java.io.IOException异常:打开失败:EINVAL(无效参数)
07-09 13:57:13.479:W / System.err的(5715):在java.io.File.createNewFile(File.java:940)
07-09 13:57:13.479:W / System.err的(5715):在com.moveinblue.planner.asynctask.ImageDownloader.writeToSD(ImageDownloader.java:459)
07-09 13:57:13.479:W / System.err的(5715):在com.moveinblue.planner.asynctask.ImageDownloader.access $ 2(ImageDownloader.java:448)
07-09 13:57:13.479:W / System.err的(5715):在com.moveinblue.planner.asynctask.ImageDownloader$BitmapDownloaderTask.onPostExecute(ImageDownloader.java:270)
07-09 13:57:13.479:W / System.err的(5715):在com.moveinblue.planner.asynctask.ImageDownloader$BitmapDownloaderTask.onPostExecute(ImageDownloader.java:1)
07-09 13:57:13.479:W / System.err的(5715):在android.os.AsyncTask.finish(AsyncTask.java:602)
07-09 13:57:13.479:W / System.err的(5715):在android.os.AsyncTask.access $ 600(AsyncTask.java:156)
07-09 13:57:13.479:W / System.err的(5715):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:615)
07-09 13:57:13.479:W / System.err的(5715):在android.os.Handler.dispatchMessage(Handler.java:99)
07-09 13:57:13.479:W / System.err的(5715):在android.os.Looper.loop(Looper.java:137)
07-09 13:57:13.479:W / System.err的(5715):在android.app.ActivityThread.main(ActivityThread.java:4575)
07-09 13:57:13.489:W / System.err的(5715):在java.lang.reflect.Method.invokeNative(本机方法)
07-09 13:57:13.489:W / System.err的(5715):在java.lang.reflect.Method.invoke(Method.java:511)
07-09 13:57:13.489:W / System.err的(5715):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789)
07-09 13:57:13.489:W / System.err的(5715):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-09 13:57:13.489:W / System.err的(5715):在dalvik.system.NativeStart.main(本机方法)
07-09 13:57:13.489:W / System.err的(5715):libcore.io.ErrnoException:产生的原因打开失败:EINVAL(无效参数)
07-09 13:57:13.489:W / System.err的(5715):在libcore.io.Posix.open(本机方法)
07-09 13:57:13.489:W / System.err的(5715):在libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-09 13:57:13.489:W / System.err的(5715):在java.io.File.createNewFile(File.java:933)
07-09 13:57:13.489:W / System.err的(5715):15 ...更多
字符串网址
含有非法字符的文件名。您将通过删除非法字符需要清理的文件名。
This is my code:
private boolean writeToSD(Bitmap bm, String url) {
if (canIWriteOnSD()) {
File sd = Environment.getExternalStorageDirectory();
File dest = new File(sd, "MoveInBlue/");
try {
url = urlCleaner(url);
if (!dest.exists()) {
dest.mkdir();
}
File file = new File(dest, url + ".png");
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
return true;
} catch (Exception e) {
e.printStackTrace();
// Do nothing
}
}
return false;
}
PROBLEM SOLVED:
urlCleaner now returns url.substring(url.lastIndexOf('?')+1), and everything works as intended.
The exception is actually thrown at file.createNewFile();
and I don't really know why.
Thank you very much.
(urlCleaner just removes the http://... from the url and leaves the php tags)
Here's the LogCat:
07-09 13:57:13.479: W/System.err(5715): java.io.IOException: open failed: EINVAL (Invalid argument)
07-09 13:57:13.479: W/System.err(5715): at java.io.File.createNewFile(File.java:940)
07-09 13:57:13.479: W/System.err(5715): at com.moveinblue.planner.asynctask.ImageDownloader.writeToSD(ImageDownloader.java:459)
07-09 13:57:13.479: W/System.err(5715): at com.moveinblue.planner.asynctask.ImageDownloader.access$2(ImageDownloader.java:448)
07-09 13:57:13.479: W/System.err(5715): at com.moveinblue.planner.asynctask.ImageDownloader$BitmapDownloaderTask.onPostExecute(ImageDownloader.java:270)
07-09 13:57:13.479: W/System.err(5715): at com.moveinblue.planner.asynctask.ImageDownloader$BitmapDownloaderTask.onPostExecute(ImageDownloader.java:1)
07-09 13:57:13.479: W/System.err(5715): at android.os.AsyncTask.finish(AsyncTask.java:602)
07-09 13:57:13.479: W/System.err(5715): at android.os.AsyncTask.access$600(AsyncTask.java:156)
07-09 13:57:13.479: W/System.err(5715): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
07-09 13:57:13.479: W/System.err(5715): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 13:57:13.479: W/System.err(5715): at android.os.Looper.loop(Looper.java:137)
07-09 13:57:13.479: W/System.err(5715): at android.app.ActivityThread.main(ActivityThread.java:4575)
07-09 13:57:13.489: W/System.err(5715): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 13:57:13.489: W/System.err(5715): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 13:57:13.489: W/System.err(5715): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-09 13:57:13.489: W/System.err(5715): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-09 13:57:13.489: W/System.err(5715): at dalvik.system.NativeStart.main(Native Method)
07-09 13:57:13.489: W/System.err(5715): Caused by: libcore.io.ErrnoException: open failed: EINVAL (Invalid argument)
07-09 13:57:13.489: W/System.err(5715): at libcore.io.Posix.open(Native Method)
07-09 13:57:13.489: W/System.err(5715): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-09 13:57:13.489: W/System.err(5715): at java.io.File.createNewFile(File.java:933)
07-09 13:57:13.489: W/System.err(5715): ... 15 more
The string url
contains illegal characters for a filename. You'll need to cleanup the filename by removing the illegal characters.
这篇关于java.io.IOException异常:打开失败:EINVAL(无效参数)保存图像到外部存储时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!