2015-06-27 135 views
2

我的應用(Android API 15)生成一張照片並將其存儲在內部存儲器的文件夾中。現在,我想將此文件複製到外部存儲器內的另一個文件夾,例如/sdcard/myapp。我嘗試以下方法:在Android中將文件從內部複製到外部存儲

方法#1:

private void copyFile(File src, File dst) throws IOException { 

    File from = new File(src.getPath()); 
    File to = new File(dst.getPath()); 
    from.renameTo(to); 
} 

方法2:

private void copyFile(File src, File dst) throws IOException { 

    FileChannel inChannel = null; 
    FileChannel outChannel = null; 

    try { 
     inChannel = new FileInputStream(src).getChannel(); 
     outChannel = new FileOutputStream(dst).getChannel(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    try { 
     inChannel.transferTo(0, inChannel.size(), outChannel); 
    } finally { 
     if (inChannel != null) 
      inChannel.close(); 
     if (outChannel != null) 
      outChannel.close(); 
    } 
} 

方法3:

private void copyFile(File src, File dst) throws IOException { 
    FileInputStream inStream = new FileInputStream(src); 

    if (!dst.exists()) { 
     dst.mkdir(); 
    } 

    if (!dst.canWrite()) { 
     System.out.print("CAN'T WRITE"); 
     return; 
    } 

    FileOutputStream outStream = new FileOutputStream(dst); 
    FileChannel inChannel = inStream.getChannel(); 
    FileChannel outChannel = outStream.getChannel(); 
    inChannel.transferTo(0, inChannel.size(), outChannel); 
    inStream.close(); 
    outStream.close(); 
} 

這些方法都沒有解決我的任務。在檢查了一些相關的話題,而且我發現唯一的建議是驗證

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

執意AndroidManifest.xml和它確實存在。

方法#1完成執行,但沒有文件夾和文件被複制。

方法#2,應用程序失敗,並在outChannel = new FileOutputStream(dst).getChannel();異常java.lang.NullPointerException,但對象dst爲不是空。

辦法#3,我決定以驗證目標對象存在,它會創建如果需要的文件夾,但是當我檢查,如果我能寫,檢查返回false

我嘗試了一些其他方法,它們成功地創建了一個空文件夾,但沒有真正複製文件。

由於這是我邁向Android的第一步,我覺得我錯過了一些小事情。請指點一下,如何將文件從一個文件夾複製到Android中的另一個文件夾,包括文件從內部移動到外部內存。

謝謝。

+0

可能是路徑問題? – Proxytype

+0

@Proxytype,關於路徑,我這樣做:'String dstPath = Environment.getExternalStorageDirectory()+ File.separator +「myapp」+ File.separator +「IMG_」+ timeStamp +「.jpg」; 文件dst =新文件(dstPath);'。我的目的地路徑應該包含文件的名稱還是文件夾?爲什麼'new FileOutputStream(dst).getChannel();'即使'dst'被填充並且存儲上有可用空間也會返回null? –

+0

嘗試在寫入之前創建目標文件,File dest = new File(path);檢查它是否在設備上創建...也給它的名稱..文件到=新文件(dst.getPath()+「/ myname」); – Proxytype

回答

6

我解決了我的問題。問題是在目標路徑中,在原有代碼:

File dst = new File(dstPath); 

變量dstPath有充分的目標路徑,包括文件名,這是錯誤的。下面是正確的代碼片段:

String dstPath = Environment.getExternalStorageDirectory() + File.separator + "myApp" + File.separator; 
File dst = new File(dstPath); 

exportFile(pictureFile, dst); 

private File exportFile(File src, File dst) throws IOException { 

    //if folder does not exist 
    if (!dst.exists()) { 
     if (!dst.mkdir()) { 
      return null; 
     } 
    } 

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    File expFile = new File(dst.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"); 
    FileChannel inChannel = null; 
    FileChannel outChannel = null; 

    try { 
     inChannel = new FileInputStream(src).getChannel(); 
     outChannel = new FileOutputStream(expFile).getChannel(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    try { 
     inChannel.transferTo(0, inChannel.size(), outChannel); 
    } finally { 
     if (inChannel != null) 
      inChannel.close(); 
     if (outChannel != null) 
      outChannel.close(); 
    } 

    return expFile; 
} 

感謝您的提示。

+0

謝謝。你的方式比寫緩衝區快得多。 – TruongHieu

相關問題