2014-04-14 66 views
0

我使用下面的代碼保存圖片到SD卡,但我不能這樣做,無法在SD卡的Android保存圖像

這裏是我的代碼

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 
+0

什麼是你的根路徑。 –

+0

root = new File(Environment.getExternalStorageDirectory() \t \t \t \t + File.separator +「Android/data/com.golustedge.poetry」+ filecatpath); 它正確地在根路徑創建文件夾(root.mkdirs();)。 – user934820

+0

和你的filecatpath是什麼。 –

回答

0

添加sdImageMainDirectory.createNewFile()創建文件之前打開它

添加權限

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

java.io.IOException:打開失敗:EINVAL(無效參數) 04-14 13:50:07.234:W/System.err(2388):\t at java.io.File.createNewFile(File.java:940) – user934820

+0

你是否擁有清單文件的權限? –

+0

是的我有<使用權限android:name =「android.permission.WRITE_EXTERNAL_STORAGE」/> – user934820

0

add sdImageMainDirectory.createNewFile();在第三行之前。另外,請檢查您是否有權限寫外置SD卡

+0

是的,我有權限+我重新啓動了設置,並且還檢查了sdcard它創建了新文件夾(root.mkdirs();)路徑但不能創建圖像 – user934820

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(); 
} 
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(),但是有許多設備使文件名無效h特殊字符,所以它給無效的參數錯誤。

+0

同樣的錯誤。沒有改變。我將路徑更改爲靜態路徑,但似乎無法在SD卡上創建文件夾而無任何問題的文件。 – user934820

+0

檢查我編輯的答案。 –