1

我在将文件从文件夹复制到其他文件夹时遇到一些问题;两者都在 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);
4

3 回答 3

4

您还应该添加以下权限<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

于 2013-07-25T06:37:24.117 回答
0

你好这样用..

    public void realizarBackup()
  {
          String nombrebd = "BRSAndroid.db";

         File bdoriginal = new File(Environment.getExternalStorageDirectory() + "/BRSAndroid/" + nombrebd);
        File directoriobackup = new File(Environment.getExternalStorageDirectory() + "/BRSAndroid/Backup/");
        File backup = new File(Environment.getExternalStorageDirectory() + "/BSRAndroid/Backup/" + nombrebd);

           if(!directoriobackup .exists())
              {
                  directoriobackup .mkdir();
              }


              if (!backup.exists()) {
                      try {
                          backup.createNewFile();
                          FileChannel src = new FileInputStream(bdoriginal).getChannel();
                          FileChannel dst = new FileOutputStream(backup).getChannel();
                          dst.transferFrom(src, 0, src.size());
                          src.close();
                          dst.close();

                      } catch (IOException e) {
                          e.printStackTrace();
                      }
              }
    }

希望这可能对您有所帮助。

于 2013-07-25T06:40:57.310 回答
0

这个片段实际上没有意义:

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.");

当然,您将无法读取或写入尚未创建的文件!你的意思是像这样检查后吗?

backup.createNewFile(); // create, THEN check if you can read and write
if (!backup.canRead())
    Log.e("Error", "No tiene permisos para leer.");
if (!backup.canWrite())
    Log.e("Error", "No tiene permisos para escribir.");

或者,也许你的意思是检查你是否可以阅读bdoriginal,或者检查你是否可以读写directoriobackup

于 2013-07-26T16:13:20.663 回答