无法在SDCard Android上保存图像

时间:2014-04-14 08:46:11

标签: java android

我使用下面的代码将图像保存到SD卡但我无法这样做

这是我的代码

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

这是错误

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

4 个答案:

答案 0 :(得分:1)

更改根路径:

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

到此:

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

使用此方法检查目录isExist()与否,但是有许多设备使用特殊字符使文件名无效,因此它会给出无效的参数错误。

答案 1 :(得分:0)

在打开文件之前添加sdImageMainDirectory.createNewFile()以创建文件

添加权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 2 :(得分:0)

添加sdImageMainDirectory.createNewFile();在第三行之前。还要检查您是否有权写入外部SD卡

答案 3 :(得分:0)

请执行以下操作 -

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