Android - Sdcard不会创建/复制文件

时间:2013-07-25 06:29:21

标签: android sd-card

我在将文件从文件夹复制到其他文件夹时遇到一些问题;两者都在SD卡中。

我想从/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

解决了从File到String的路径更改。

String ruta = Environment.getExternalStorageDirectory().getPath()+"/BRSAndroid/"
String nombrebd = "BRSAndroid.db";
File bdoriginal = new File(ruta + nombrebd);
File backup = new File(ruta + "Backup/" + nombrebd);

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分: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();
                      }
              }
    }

希望这对你有所帮助。

答案 2 :(得分: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