FileNotFoundException on API 16… here is a solution to the problem.
FileNotFoundException on API 16
I tried saving the file to external storage and it worked (tested on API 19-22) but on API 16 it crashed.
The code is as follows:
//save image to SD
private void saveToExternal(Bitmap bitmap,String fileName){
File myDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File checkFile = new File(myDir,fileName+".png");
if(checkFile.exists()){
checkFile = new File(myDir,fileName+"(1).png");
}
if (myDir!=null && myDir.isDirectory() && myDir.exists()) {
try {
FileOutputStream out = new FileOutputStream(checkFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
Log.d("myTag","write file to external");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
exception:
08-01 07:38:24.708 2159-2159/? W/System.err﹕ java.io.FileNotFoundException: /mnt/sdcard/Android/data/com.dandewine.user.tocleveroad/files/Pictures/milk | The Balanced Blonde.png: open failed: EINVAL (Invalid argument)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:416)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at com.dandewine.user.tocleveroad.adapters.ResultsImageAdapter.saveToExternal(ResultsImageAdapter.java:174)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at com.dandewine.user.tocleveroad.adapters.ResultsImageAdapter.access$500(ResultsImageAdapter.java:45)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at com.dandewine.user.tocleveroad.adapters.ResultsImageAdapter$3.onLoadingComplete(ResultsImageAdapter.java:127)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at com.nostra13.universalimageloader.core.DisplayBitmapTask.run(DisplayBitmapTask.java:72)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:615)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:4745)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EINVAL (Invalid argument)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at libcore.io.Posix.open(Native Method)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:400)
08-01 07:38:24.708 2159-2159/? W/System.err﹕ ... 15 more
FileOutputStream out = new FileOutputStream(checkFile);
Any suggestions on how to properly save files to SD?
Solution
This
milk | The Balanced Blonde.png
Not a good file name. It contains a |
with other responsibilities ( pipe). Newer versions of Android may handle this correctly, but it’s best not to use it in the file name.