我在将文件从文件夹复制到其他文件夹时遇到一些问题;两者都在 SDCard 中。
我想从复制/sdcard/folder1/file.db
到/sdcard/folder1/folder2
(如果不存在则创建)/file.db
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
我的代码
public void realizarBackup()
{
String nombrebd = "BRSAndroid.db";
File sd = new File(Environment.getExternalStorageDirectory().getPath());
if (sd.exists() && sd.canWrite()){
File bdoriginal = new File(sd.getAbsolutePath() + "/BRSAndroid/" + nombrebd);
File directoriobackup = new File(sd.getAbsolutePath() + "/BRSAndroid/Backup/");
File backup = new File(sd.getAbsolutePath() + "/BSRAndroid/Backup/" + nombrebd);
directoriobackup.mkdir(); // It creates this folder, this doesn't fail.
if (directoriobackup.exists())
{
try {
if(backup.canRead()){
if(backup.canWrite())
backup.createNewFile();
else
Log.e("Error", "No tiene permisos para escribir.");
} else // STOPS HERE
Log.e("Error", "No tiene permisos para leer.");
} catch (IOException e1) {
Log.e("Error", "Error al crear el fichero BRSAndroid en la carpeta de destino.");
}
if(backup.exists()) {
try {
InputStream entrada = new FileInputStream(bdoriginal);
OutputStream salida = new FileOutputStream(backup);
byte[] buf = new byte[1024];
int longitud;
while((longitud = entrada.read(buf)) > 0){
salida.write(buf, 0, longitud);
}
entrada.close();
salida.close();
} catch (Exception e){
Log.e("Error", "Error a la hora de hacerse el backup.");
}
}
}
else
{
Log.e("Error", "Error en la creación del directorio /Backup/.");
}
}
}
我究竟做错了什么?我被困在这里,我不能继续前进。
我发现备份没有读取权限。
LogCat 日志:
07-25 09:37:05.578: W/System.err(16602): java.io.IOException: open failed: ENOENT (No such file or directory)
07-25 09:37:05.578: W/System.err(16602): at java.io.File.createNewFile(File.java:940)
07-25 09:37:05.586: W/System.err(16602): at NS.Android.Clientes.ClientesActivity.realizarBackup(ClientesActivity.java:1602)
07-25 09:37:05.586: W/System.err(16602): at NS.Android.Clientes.ClientesActivity$4.onClick(ClientesActivity.java:398)
07-25 09:37:05.586: W/System.err(16602): at android.view.View.performClick(View.java:4222)
07-25 09:37:05.586: W/System.err(16602): at android.view.View$PerformClick.run(View.java:17337)
07-25 09:37:05.594: W/System.err(16602): at android.os.Handler.handleCallback(Handler.java:615)
07-25 09:37:05.594: W/System.err(16602): at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 09:37:05.594: W/System.err(16602): at android.os.Looper.loop(Looper.java:137)
07-25 09:37:05.594: W/System.err(16602): at android.app.ActivityThread.main(ActivityThread.java:4895)
07-25 09:37:05.594: W/System.err(16602): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 09:37:05.601: W/System.err(16602): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 09:37:05.601: W/System.err(16602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
07-25 09:37:05.601: W/System.err(16602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
07-25 09:37:05.601: W/System.err(16602): at dalvik.system.NativeStart.main(Native Method)
07-25 09:37:05.609: W/System.err(16602): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
07-25 09:37:05.609: W/System.err(16602): at libcore.io.Posix.open(Native Method)
07-25 09:37:05.609: W/System.err(16602): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-25 09:37:05.609: W/System.err(16602): at java.io.File.createNewFile(File.java:933)
07-25 09:37:05.617: W/System.err(16602): ... 13 more
解决了从文件到字符串的更改路径。
String ruta = Environment.getExternalStorageDirectory().getPath()+"/BRSAndroid/"
String nombrebd = "BRSAndroid.db";
File bdoriginal = new File(ruta + nombrebd);
File backup = new File(ruta + "Backup/" + nombrebd);