Android java.io.ioexception:打开失败:将图像保存到外部存储时的einval(无效参数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11394616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:15:49  来源:igfitidea点击:

java.io.ioexception: open failed: einval (Invalid argument) when saving a image to external storage

androidfile-ioioexception

提问by Charlie-Blake

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.

urlCleaner 现在返回 url.substring(url.lastIndexOf('?')+1),一切都按预期工作。

The exception is actually thrown at file.createNewFile();and I don't really know why.

实际上抛出了异常,file.createNewFile();我真的不知道为什么。

Thank you very much.

非常感谢。

(urlCleaner just removes the http://... from the url and leaves the php tags)

(urlCleaner 只是从 url 中删除了 http://... 并留下了 php 标签)

Here's the LogCat:

这是 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(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.access0(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

回答by David Wasser

The string urlcontains illegal characters for a filename. You'll need to cleanup the filename by removing the illegal characters.

该字符串url包含文件名的非法字符。您需要通过删除非法字符来清理文件名。

回答by Alejandra

Really tricky error since Android 28 works just fine when trying to write a filename with ? or : in the name, but Android 22 for instance would blow up. Just run a couple of replacements in the filename like:

非常棘手的错误,因为 Android 28 在尝试使用 ? 或 : 在名称中,但例如 Android 22 会爆炸。只需在文件名中运行几个替换,例如:

File(filename.replace(":", "").replace("?", "") [...])

and then you're good to run

然后你就可以跑了

file.createNewFile()