我有两个位于android / data系统深处的文件,下面是这样一个文件的例子。
/storage/emulated/0/Android/data/mytest.com.test/files/Documents/Test/Bin/data.dat
我想将它们都复制到内部存储上的某个位置。 现在stackoverflow是一个很好的社区,有很多例子,因此我已经搜索了一些例子它是如何完成的但不幸的是它没有用。
logcat的:
V/debug: Copy file failed. Source file missing.
我验证了这一点,但文件肯定存在,目标目录已创建,但为空。 有人可以帮助我吗?
主要备份方法:
public void backupFavorites() {
String folder1, folder2, folder3, folder4;
//Set target directory
String path = Utils.getDownloadDestination(this) + "/FavoritesBackup/Bin/";
File rootPath = new File(path);
if (!rootPath.exists())
rootPath.mkdirs();
//Prepare Sourcefile 1
folder1 = (rootPath + "/" + "data.dat");
File sdcardData = new File (this.getExternalFilesDir
("Documents"), "MyTestApp");
String pathdata = sdcardData.getPath() + "/Bin/data.dat";
File data = new File(pathdata);
//Prepare Sourcefile 2
folder2 = (rootPath + "/" + "trackerdata.dat");
File sdcardTracker = new File(this.getExternalFilesDir
("Documents"), "MyTestApp");
String pathtracker = sdcardTracker.getPath() + "/Bin/trackerdata.dat";
File tracker = new File(pathtracker);
if (trackerDataExists(this)) {
ArrayList<File> sourceFiles = new ArrayList<>();
sourceFiles.add(data);
sourceFiles.add(tracker);
ArrayList<String> destFiles = new ArrayList<>();
destFiles.add(folder1);
destFiles.add(folder2);
for (int i = 0; i < sourceFiles.size(); i++) {
for (int p = 0; p < destFiles.size(); p++) {
try {
copyFiles(sourceFiles.get(i), destFiles.get(p));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Toast.makeText(this, "Backup created", Toast.LENGTH_SHORT).show();
}
}
复制文件的代码:
void copyFiles(File sourceLocation, String targtLocation) throws IOException {
if (sourceLocation.exists()) {
FileInputStream fin = null;
FileOutputStream fout = null;
Log.i("debug", "source " + sourceLocation);
Log.i("debug", "des " + targtLocation);
try {
fin = new FileInputStream(sourceLocation);
new File(String.valueOf(targtLocation)).delete();
fout = new FileOutputStream(targtLocation, false);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Copy the bits from instream to outstream
byte[] buf = new byte[2048];
int len;
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fout);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fin);
while ((len = bufferedInputStream.read(buf)) > 0) {
bufferedOutputStream.write(buf, 0, len);
}
fin.close();
bufferedOutputStream.close();
fout.close();
Log.e("debug", "Copy file successful.");
} else {
Log.v("debug", "Copy file failed. Source file missing.");
}
}
获取下载目的地的代码:
public static String getDownloadDestination(Context mCon) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mCon);
return pref.getString("downloadFolder", MYDESTINATION.getAbsolutePath());
}
public static final File MYDESTINATION = new File(Environment.getExternalStorageDirectory(),
"Test");
答案 0 :(得分:0)
我自己解决了。如果有人遇到同样的问题,下面是工作代码。
public void backupFiles() {
if (DataExists(this)) {
ArrayList<String> sourceFiles = new ArrayList<>();
sourceFiles.add("codex.dat");
sourceFiles.add("ids.dat");
sourceFiles.add("data.dat");
for (int i = 0; i < sourceFiles.size(); i++) {
try {
copyFiles(sourceFiles.get(i));
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(this, "Backup of FAVORITES created", Toast.LENGTH_SHORT).show();
}
}
public void copyFiles(String sourceLocation) throws IOException {
if (!sourceLocation.isEmpty()) {
FileInputStream fin = null;
FileOutputStream fout = null;
String path = Environment.getExternalStorageDirectory() + Utils.getDownloadDestination
(this) + "/TestBackup/Bin/";
File rootPath = new File(path);
if (!rootPath.exists())
rootPath.mkdirs();
File sdcardData = new File(this.getExternalFilesDir
("Documents"), "MyTest");
String pathdata = sdcardData.getPath() + "/Bin/" + sourceLocation;
File data = new File(pathdata);
Log.i("debug", "source " + pathData);
Log.i("debug", "des " + path + sourceLocation);
try {
fin = new FileInputStream(data);
File outFile = new File(path, sourceLocation);
fout = new FileOutputStream(outFile, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fout);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fin);
while ((len = bufferedInputStream.read(buf)) > 0) {
bufferedOutputStream.write(buf, 0, len);
}
fin.close();
bufferedOutputStream.close();
fout.close();
//Comment out to delete originals!
//data.delete();
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
scanIntent.setData(Uri.parse(sourceLocation));
sendBroadcast(scanIntent);
Log.e("debug", "Copy file successful.");
} else {
Log.v("debug", "Copy file failed. Source file missing." + sourceLocation);
}
}