我需要從一個位置上的用戶SD卡將文件移動到另一個位置上的SD卡移動在Android上
目前,我與File.renameTo
例如這樣一個文件從sdcard/test/one.txt到sdcard/test2/two.txt
某些用戶報告文件移動功能不起作用。
How to copy files from 'assets' folder to sdcard?
那麼什麼是一個目錄中的文件移動到另一個對SD卡的最佳方式:
我碰到下面的鏈接來了?
我需要從一個位置上的用戶SD卡將文件移動到另一個位置上的SD卡移動在Android上
目前,我與File.renameTo
例如這樣一個文件從sdcard/test/one.txt到sdcard/test2/two.txt
某些用戶報告文件移動功能不起作用。
How to copy files from 'assets' folder to sdcard?
那麼什麼是一個目錄中的文件移動到另一個對SD卡的最佳方式:
我碰到下面的鏈接來了?
嘗試複製這些代碼並檢查文件並刪除原來的文件。
private void backup(File sourceFile)
{
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel in = null;
FileChannel out = null;
try
{
File backupFile = new File(backupDirectory.getAbsolutePath() + seprator + sourceFile.getName());
backupFile.createNewFile();
fis = new FileInputStream(sourceFile);
fos = new FileOutputStream(backupFile);
in = fis.getChannel();
out = fos.getChannel();
long size = in.size();
in.transferTo(0, size, out);
}
catch (Throwable e)
{
e.printStackTrace();
}
finally
{
try
{
if (fis != null)
fis.close();
}
catch (Throwable ignore)
{}
try
{
if (fos != null)
fos.close();
}
catch (Throwable ignore)
{}
try
{
if (in != null && in.isOpen())
in.close();
}
catch (Throwable ignore)
{}
try
{
if (out != null && out.isOpen())
out.close();
}
catch (Throwable ignore)
{}
}
}
爲什麼不能使用rename
?
File sd=Environment.getExternalStorageDirectory();
// File (or directory) to be moved
String sourcePath="/.Images/"+imageTitle;
File file = new File(sd,sourcePath);
// Destination directory
boolean success = file.renameTo(new File(sd, imageTitle));
我知道這個問題已經很久很久以前回答,但我發現拷貝整個文件是一個嚴酷的方法...... 這裏是我做的,如果有人需要它:
static public boolean moveFile(String oldfilename, String newFolderPath, String newFilename) {
File folder = new File(newFolderPath);
if (!folder.exists())
folder.mkdirs();
File oldfile = new File(oldfilename);
File newFile = new File(newFolderPath, newFilename);
if (!newFile.exists())
try {
newFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return oldfile.renameTo(newFile);
}
感謝,那很好用 – user1177292 2012-02-03 22:29:46