我試圖以編程方式將文件從Android內部存儲器移至SD卡中的現有目錄。
我試過兩種方法。 在第一個我用File.renameTo:以編程方式將文件從緩存目錄移至SDCard
String destName = externalDirPath + File.separatorChar + destFileName;
File originFile = new File(cacheDirPath + File.separatorChar + originalfileName);
originFile.renameTo(new File(destName));
在我使用的調用Runtime.getRuntime()其他:
Process p = Runtime.getRuntime().exec("/system/bin/sh -");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
String command = "cp " + cacheDirPath + "/" + originalfileName+ " " + externalDirPath + "/" + destFileName+ "\n";
os.writeBytes(command);
與他們兩個不工作..
任何建議?
不要嘗試使用'cp',因爲它甚至不存在於股票設備上,並且運行時執行的東西沒有官方支持。如何從android java中複製文件已經在這裏被覆蓋了很多次,我們不需要另一個問題。 –
嗨丹尼,我想複製手機內存的所有內容到SD卡。你可以給我一些關於這個問題的代碼。 – 2012-06-19 04:00:43
親愛的@Naseerkhan,我是按照MacGyver的建議做的,將輸入文件的內容複製到'byte []'中,然後將其寫入輸出文件。例如,您可以看到: 'public static void copy(File src,File dst)throws IOException InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst); //將字節從in傳輸到out byte [] buf = new byte [1024]; int len; ((len = in.read(buf))> 0){out.write(buf,0,len); } in.close(); out.close(); }' –