2012-06-29 52 views
-1

我想複製從應用程序的路徑PDF文件(/數據/數據/包的名稱),以sdcard.for,我已經準備好以下,如何將文件從應用程序包路徑複製到SD卡?

try { 
     source = new FileInputStream(sourceFile).getChannel(); 
     destination = new FileOutputStream(destFile).getChannel(); 
     destination.transferFrom(source, 0, source.size()); 
    } 
    finally { 
     if(source != null) { 
      source.close(); 
     } 
     if(destination != null) { 
      destination.close(); 
     } 
    } 

它不能正常工作,請您及時的幫助。

+0

這樣的支持方式是把你的包中的資產文件夾興趣文件,並使用和使用assetManager.open()來獲取輸入流如http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard。黑客解決方案是要知道apk是zip文件的一種形式,並用zip文件類打開它。 –

回答

1

下面是示例代碼複製文件

private static void copyfile(String srFile, String dtFile){ 
     try{ 
      File f1 = new File(Source Fine Name); 
      File f2 = new File(Destination File Name); 
      InputStream in = new FileInputStream(f1); 

//     If you want to append the file. 
//   OutputStream out = new FileOutputStream(f2,true); 

      //For Overwrite the file. 
      OutputStream out = new FileOutputStream(f2); 

      byte[] buf = new byte[1024]; 
      int len; 
      while ((len = in.read(buf)) > 0){ 
       out.write(buf, 0, len); 
      } 
      in.close(); 
      out.close(); 
      System.out.println("File copied."); 
     } 
     catch(FileNotFoundException ex){ 
      System.out.println(ex.getMessage()); 

     } 
     catch(IOException e){ 
      System.out.println(e.getMessage());   
     } 
    } 
+0

我得到這個/mnt/sdcard/help_es.pdf:打開失敗:EACCES(權限被拒絕) –

+0

你給清單文件的權限 – rajpara

相關問題