0

I am using below code to save images to sdcard but I am unable to do so and

Here is my code

try {
                root.mkdirs();
                File sdImageMainDirectory = new File(root, myid + "__" + fileName);
                outputFileUri = Uri.fromFile(sdImageMainDirectory);
                fOut = new FileOutputStream(sdImageMainDirectory);          
                bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);              
                fOut.flush();
                fOut.close();
                Toast.makeText(this, "Image Saved",Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            }

Here is error

04-14 13:34:07.723: W/System.err(23073): java.io.FileNotFoundException: /mnt/sdcard/Android/data/com.goldenedge.poetry/Urdu/Festive Poetry/4__311159_248676158509124_848180994_n.jpg
04-14 13:34:07.724: W/System.err(23073): : open failed: EINVAL (Invalid argument)
04-14 13:34:07.725: W/System.err(23073):    at libcore.io.IoBridge.open(IoBridge.java:448)
04-14 13:34:07.726: W/System.err(23073):    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
04-14 13:34:07.726: W/System.err(23073):    at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
04-14 13:34:07.726: W/System.err(23073):    at com.golden.ViewImage.onOptionsItemSelected(ViewImage.java:106)
04-14 13:34:07.727: W/System.err(23073):    at android.app.Activity.onMenuItemSelected(Activity.java:2526)
04-14 13:34:07.727: W/System.err(23073):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:966)
04-14 13:34:07.727: W/System.err(23073):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
04-14 13:34:07.727: W/System.err(23073):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
04-14 13:34:07.728: W/System.err(23073):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
04-14 13:34:07.728: W/System.err(23073):    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:468)
04-14 13:34:07.728: W/System.err(23073):    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:126)
04-14 13:34:07.728: W/System.err(23073):    at android.view.View$PerformClick.run(View.java:14155)
04-14 13:34:07.729: W/System.err(23073):    at android.os.Handler.handleCallback(Handler.java:605)
04-14 13:34:07.729: W/System.err(23073):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 13:34:07.729: W/System.err(23073):    at android.os.Looper.loop(Looper.java:137)
04-14 13:34:07.730: W/System.err(23073):    at android.app.ActivityThread.main(ActivityThread.java:4666)
04-14 13:34:07.730: W/System.err(23073):    at java.lang.reflect.Method.invokeNative(Native Method)
04-14 13:34:07.730: W/System.err(23073):    at java.lang.reflect.Method.invoke(Method.java:511)
04-14 13:34:07.731: W/System.err(23073):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
04-14 13:34:07.731: W/System.err(23073):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
04-14 13:34:07.731: W/System.err(23073):    at dalvik.system.NativeStart.main(Native Method)
04-14 13:34:07.732: W/System.err(23073): Caused by: libcore.io.ErrnoException: open failed: EINVAL (Invalid argument)
04-14 13:34:07.733: W/System.err(23073):    at libcore.io.Posix.open(Native Method)
04-14 13:34:07.733: W/System.err(23073):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
04-14 13:34:07.733: W/System.err(23073):    at libcore.io.IoBridge.open(IoBridge.java:432)
04-14 13:34:07.734: W/System.err(23073):    ... 20 more
user934820
  • 1,162
  • 3
  • 17
  • 48

4 Answers4

1

Change your root Path :

root = new File(Environment.getExternalStorageDirectory() + File.separator + "Android/data/com.goldenedge.poetry" + filecatpath);

to this :

    root = new File(getFullFilePath(Environment.getExternalStorageDirectory()+"/Android/data/com.goldenedge.poetry/Urdu/Festive Poetry","4__311159_248676158509124_848180994_n.jpg ")); 

public String getFullFilePath(String filePath,String filename) {

        File dir = new File(filePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        return filePath + filename;
    }

use this method to check directory isExist() or not, but there are many devices which invalidated file name with special characters so it give invalid arguments error.

Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
  • same error. nothing changed. I changed the path to static one but it seems that I am unable to create a file on SDCard where folders are created without a problem. – user934820 Apr 14 '14 at 09:01
0

Add sdImageMainDirectory.createNewFile() to create file prior to open it

Add permissions

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Boban S.
  • 1,662
  • 13
  • 16
  • java.io.IOException: open failed: EINVAL (Invalid argument) 04-14 13:50:07.234: W/System.err(2388): at java.io.File.createNewFile(File.java:940) – user934820 Apr 14 '14 at 08:50
  • check this answer http://stackoverflow.com/questions/11394616/java-io-ioexception-open-failed-einval-invalid-argument-when-saving-a-image you have illegal characters in filename – Boban S. Apr 14 '14 at 08:52
  • if you please more specific. Here is file path and name:/mnt/sdcard/Android/data/com.goldenedge.poetry/Urdu/Festive Poetry/4__311159_248676158509124_848180994_n.jpg please tell me which character is illegal? – user934820 Apr 14 '14 at 08:54
  • try without double '__', i'm not sure if this is the problem, but try it – Boban S. Apr 14 '14 at 09:00
  • maybe this answer can help you http://stackoverflow.com/questions/20310904/filename-chars-cause-opening-failed-einval-invalid-argument – Boban S. Apr 14 '14 at 09:01
0

add sdImageMainDirectory.createNewFile(); before third line. Also check if you have permission to write to external sd card

jasleen
  • 61
  • 1
  • 4
  • yes I have permission + I restarted the set and also checked the sdcard it creates new folder (root.mkdirs();) at that path but not create image – user934820 Apr 14 '14 at 08:52
0

Do like the following -

String pathToExternalStorage = Environment.getExternalStorageDirectory().toString();
File root = new File(pathToExternalStorage + "/saved_images");

if (!root.exists())
    root.mkdirs();

try 
{
    File sdImageMainDirectory = new File(root, myid + "__" + fileName + ".png");

    fOut = new FileOutputStream(sdImageMainDirectory);          

    bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    fOut.flush();
    fOut.close();
} 

catch (Exception e) 
{
    e.printStackTrace();
}
sjain
  • 23,126
  • 28
  • 107
  • 185