user934820
user934820

Reputation: 1178

Unable to Save Image on SDCard Android

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

Upvotes: 0

Views: 1699

Answers (4)

Harshit Rathi
Harshit Rathi

Reputation: 1862

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.

Upvotes: 1

sjain
sjain

Reputation: 23344

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();
}

Upvotes: 0

jasleen
jasleen

Reputation: 61

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

Upvotes: 0

Boban S.
Boban S.

Reputation: 1662

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" />

Upvotes: 0

Related Questions