2017-04-15 80 views
0

Im新的Android版本,我想將所有圖像文件從一個文件夾(臨時文件夾)移動到另一個文件夾(新文件夾)。我寫了一些代碼,但我得到的錯誤(一次移動成功,該文件應該是從臨時文件夾刪除)請人幫我如何將所有圖像從一個文件夾移動到另一個android

這裏我的代碼:

String timeStamp = new SimpleDateFormat("yyyyMMdd").format(new Date()); 
       File sourceStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Example/tmp"); 
       File destStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Example/"+timeStamp); 
       try { 
        copyDirectory(sourceStorageDir,destStorageDir); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 


public void copyDirectory(File sourceLocation , File targetLocation)throws IOException { 

    // FileUtils.copyFile(sourceLocation, targetLocation); 

    if (sourceLocation.isDirectory()) { 
     if (!targetLocation.exists()) { 
      targetLocation.mkdir(); 
     } 

     String[] children = sourceLocation.list(); 
     for (int i = 0; i < sourceLocation.listFiles().length; i++) { 

      copyDirectory(new File(sourceLocation, children[i]),new File(targetLocation, children[i])); 
     } 
    } else { 
     Log.d("Else", String.valueOf(targetLocation)+"-->"+String.valueOf(sourceLocation)); 

     InputStream in = new FileInputStream(sourceLocation); 

     OutputStream out = new FileOutputStream(targetLocation); 

     // Copy the bits from instream to outstream 
     byte[] buf = new byte[1024]; 
     int len; 
     while ((len = in.read(buf)) > 0) { 
      out.write(buf, 0, len); 
     } 
     in.close(); 
     out.close(); 
    } 
} 

我的logcat:

04-15 12:04:35.302 21236-21236/com.motopit W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Pictures/Example/20170415/IMG__&_20170415_104438.jpg (Not a directory) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at java.io.FileOutputStream.open(Native Method) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at java.io.FileOutputStream.<init>(FileOutputStream.java:221) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at java.io.FileOutputStream.<init>(FileOutputStream.java:169) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at com.motopit.ImageActivity.copyDirectory(ImageActivity.java:326) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at com.motopit.ImageActivity.copyDirectory(ImageActivity.java:319) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at com.motopit.ImageActivity.onOptionsItemSelected(ImageActivity.java:285) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at android.app.Activity.onMenuItemSelected(Activity.java:3208) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:408) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:198) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:107) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at android.support.v7.app.AppCompatDelegateImplV9.onMenuItemSelected(AppCompatDelegateImplV9.java:671) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:817) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:964) 
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:  at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:954) 
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:  at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624) 
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:  at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:157) 
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:  at android.view.View.performClick(View.java:5612) 
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:  at android.view.View$PerformClick.run(View.java:22288) 
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:  at android.os.Handler.handleCallback(Handler.java:751) 
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:95) 
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:  at android.os.Looper.loop(Looper.java:154) 
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:6123) 
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
04-15 12:04:35.692 21236-21269/com.motopit W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found. 

在此先感謝!

+0

從錯誤中可以看出你試圖獲取不存在的文件。 –

+0

在複製文件之前,請確保您必須檢查文件是否存在。 –

+0

我在哪裏檢查..我沒有刪除文件..該文件存在於該文件夾中 – abu

回答

0

試試這個。

public void copyFileFromDirectory(File sourceLocation, File targetLocation) 
{ 
    if (sourceLocation.isDirectory()) 
    { 
     if (!targetLocation.exists() && !targetLocation.mkdirs()) 
     { 
      try 
      { 
       throw new IOException("Directory not creating " + targetLocation.getAbsolutePath()); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
     } 
     String[] children = sourceLocation.list(); 
     for (int i = 0; i < children.length; i++) 
     { 
      copyFileFromDirectory(new File(sourceLocation, children[i]), 
        new File(targetLocation, children[i])); 
     } 
    } 
    else 
    { 
     File directory = targetLocation.getParentFile(); 
     // Check Directory is exist or not. 
     if (directory != null && !directory.exists() && !directory.mkdirs()) 
     { 
      try 
      { 
       throw new IOException("Directory not creating " + directory.getAbsolutePath()); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
     } 

     InputStream in = null; 
     OutputStream out = null; 
     try 
     { 
      in = new FileInputStream(sourceLocation); 
      out = new FileOutputStream(targetLocation); 
     } 
     catch (FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 

     byte[] buf = new byte[1024]; 
     int len; 
     try 
     { 
      while ((len = in.read(buf)) > 0) 
      { 
       out.write(buf, 0, len); 
      } 
      in.close(); 
      out.close(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 
相關問題