2015-07-19 69 views
0

我想從資產文件夾複製圖像到SD卡,但似乎並沒有複製它首次啓動。它創建文件夾好,但不會複製文件。從資產文件夾複製文件到SD卡似乎並不工作

prefs = getPreferences(Context.MODE_PRIVATE); 
    if (prefs.getBoolean("firstLaunch", true)) { 
     prefs.edit().putBoolean("firstLaunch", false).commit(); 
    File nfile=new File(Environment.getExternalStorageDirectory()+"/My Images"); 
    nfile.mkdir(); 
    } 
    AssetManager assetManager = getAssets(); 
    String[] files = null; 
    try { 
     files = assetManager.list("middle.jpg"); 
    } catch (IOException e) { 
     Log.e("tag", "Failed to get asset file list.", e); 
    } 
    for(String filename : files) { 
     InputStream in = null; 
     OutputStream out = null; 
     try { 
      in = assetManager.open(filename); 
      File outFile = new File(Environment.getExternalStorageDirectory()+ "/My Images" + filename); 
      out = new FileOutputStream(outFile); 
      copyFile(in, out); 
     } catch(IOException e) { 
      Log.e("tag", "Failed to copy asset file: " + filename, e); 
     }  
     finally { 
      if (in != null) { 
       try { 
        in.close(); 
       } catch (IOException e) { 

       } 
      } 
      if (out != null) { 
       try { 
        out.close(); 
       } catch (IOException e) { 

       } 
      } 
     } 
    } 

    private void copyFile(InputStream in, OutputStream out) { 
    // TODO Auto-generated method stub 

} 

middle.jpg是我想複製的文件。任何人都可以告訴我我做錯了什麼?

PS我在我的清單中有WRITE_EXTERNAL_STORAGE

感謝

回答

0

你忘了加/中/我的圖片結束而構建的路徑

File outFile = new File(Environment.getExternalStorageDirectory()+ "/My Images/" + filename); out = new FileOutputStream(outFile);

因爲文件名會MyImages +文件名這樣就不會存在複製。

+0

mmmm我現在真的覺得很蠢:-)感謝您的幫助:-) – Allrounder

+0

現在工作。 – Allrounder

相關問題