2012-01-30 122 views
1

我试图将我现有的数据库从我的内部存储复制到外部存储,但我遇到了一些问题。它说the file does not exist。下面是我使用的究竟是什么:从内部到外部存储的Android复制数据库

从内部的文件复制到外部存储:

private void copyFile(String src, String dst) throws IOException{ 
    FileChannel inChannel = new FileInputStream(src).getChannel(); 
    FileChannel outChannel = new FileOutputStream(dst).getChannel(); 
    try 
    { 
     inChannel.transferTo(0, inChannel.size(), outChannel); 
    } 
    finally 
    { 
     if (inChannel != null) 
      inChannel.close(); 
     if (outChannel != null) 
      outChannel.close(); 
    } 
} 

,我使用它是这样的:

copyFile("/data/data/com.example.test/databases/database.sqlite","/sdcard/.Example/Data/database.sqlite"); 

,但它没有工作,我很确定内部存储中的数据库在那里。

如果我在copyFile"/sdcard/.Example/Data/"中设置了目标文件夹,它将在.Example中创建文件Data

任何建议我失踪了?

回答

0

您是否添加了WRITE_EXTERNAL_STORAGE权限?

<?xml version="1.0" encoding="utf-8"?> 
<manifest ... 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
</manifest> 
+0

是的,我已经这样做。其实我注意到文件被复制,但是它是空的。 – 2012-01-30 16:27:51

5

使用此选项将数据库复制到SD卡。

public void copyFile() 
    { 
     try 
     { 
      File sd = Environment.getExternalStorageDirectory(); 
      File data = Environment.getDataDirectory(); 

      if (sd.canWrite()) 
      { 
       String currentDBPath = "\\data\\your.package.name\\databases\\dabase_name"; 
       String backupDBPath = "database_name"; 
       File currentDB = new File(data, currentDBPath); 
       File backupDB = new File(sd, backupDBPath); 

       if (currentDB.exists()) { 
        FileChannel src = new FileInputStream(currentDB).getChannel(); 
        FileChannel dst = new FileOutputStream(backupDB).getChannel(); 
        dst.transferFrom(src, 0, src.size()); 
        src.close(); 
        dst.close(); 
       } 
       if(bool == true) 
       { 
        Toast.makeText(Settings.this, "Backup Complete", Toast.LENGTH_SHORT).show(); 
        bool = false; 
       } 
      }    
     } 
     catch (Exception e) { 
      Log.w("Settings Backup", e); 
     } 
    } 
+0

你当前的数据库路径不适用于我。这工作:“/data/my.package.name/databases/mydbName.db” – Mike6679 2013-10-31 02:28:34

+0

“if(bool == true)”?我认为有一个初始化丢失的地方。最好的猜测是你想检查“transferFrom”长反应对原始数据库的大小,或检查,看它是否大于1见:https://developer.android.com/reference/java/nio/channels/FileChannel .html#transferFrom(java.nio.channels.ReadableByteChannel,long,long) – 2017-07-19 17:35:13

4

如果你不知道你的应用程序的路径,那么你可以使用这个:

​​

或者,如果你需要复制数据库公开“下载”文件夹中,那么你可以使用这个:

public void copyAppDbToDownloadFolder() throws IOException { 
    try { 
     File backupDB = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "toDatabaseName"); // for example "my_data_backup.db" 
     File currentDB = getApplicationContext().getDatabasePath("databaseName"); //databaseName=your current application database name, for example "my_data.db" 
     if (currentDB.exists()) { 
      FileInputStream fis = new FileInputStream(currentDB); 
      FileOutputStream fos = new FileOutputStream(backupDB); 
      fos.getChannel().transferFrom(fis.getChannel(), 0, fis.getChannel().size()); 
      // or fis.getChannel().transferTo(0, fis.getChannel().size(), fos.getChannel()); 
      fis.close(); 
      fos.close(); 
      Log.i("Database successfully", " copied to download folder"); 
      return true; 
     } else Log.i("Copying Database", " fail, database not found"); 
    } catch (IOException e) { 
     Log.d("Copying Database", "fail, reason:", e); 
    } 
} 

这是在我的Nexus 4设备上完美运行。

0

该程序可以复制指定的数据库“数据库名称”从Android应用程序与外部storage.It的内部存储创建一个名为“文件夹名称”,然后将数据库复制到一个名为“备份数据库名称”文件夹该文件夹内

1.本程序与Android兼容棉花糖
2.As MediaScannerConnection使用,可以立即从电脑windows操作系统的复制备份文件而无需重新启动您的系统
3.如果“文件夹名称“不存在它将自动创建它
4.如果您指定的备份文件名称为a lready存在,它会显示一个警告对话框

if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_DENIED){ 
    if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){ 
     ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 5); 
    } 
    else{ 
     ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},5); 
    } 
} 
else{ 
    final File backupDb=new File(Environment.getExternalStorageDirectory().getPath()+File.separator+"<Folder Name>"+File.separator+"<Backup Database Name>"); 
    final File currentDB = new File(String.valueOf(getApplicationContext().getDatabasePath("<Your Database Name>"))); 
    if(backupDb.exists()){ 
     AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this) 
       .setTitle("Alert") 
       .setMessage("File already exists.Do you want to replace it") 
       .setCancelable(false) 
       .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         if(!new File(Environment.getExternalStorageDirectory().getPath()+File.separator+"<Folder Name>").canWrite()) { 
          Toast.makeText(MainActivity.this, "Unable to write into external storage", Toast.LENGTH_SHORT).show(); 
         } 
         else{ 
          if(!currentDB.exists()){ 
           Toast.makeText(MainActivity.this, "Database doesn't exists", Toast.LENGTH_SHORT).show(); 
          } 
          else{ 
           try { 
            FileChannel src = new FileInputStream(currentDB).getChannel(); 
            FileChannel dst = new FileOutputStream(backupDb).getChannel(); 
            dst.transferFrom(src, 0, src.size()); 
            src.close(); 
            dst.close(); 
            MediaScannerConnection.scanFile(getApplicationContext(), new String[]{backupDb.toString()}, null, null); 
            Toast.makeText(MainActivity.this, "Database successfully copied to external storage", Toast.LENGTH_SHORT).show(); 
            editText.setText(null); 
           }catch(Exception e){ 
            Toast.makeText(MainActivity.this, "Got exception" + e, Toast.LENGTH_SHORT).show(); 
           } 
          } 
         } 
        } 
       }) 
       .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Toast.makeText(MainActivity.this, "Please enter another name", Toast.LENGTH_SHORT).show(); 
        } 
       }) 
       .setIcon(R.drawable.alert); 
     AlertDialog dialog = builder.create(); 
     dialog.show(); 
    } 
    else{ 
     new File(Environment.getExternalStorageDirectory().getPath()+File.separator+"<Folder Name>").mkdir(); 
     if(!new File(Environment.getExternalStorageDirectory().getPath()+File.separator+"<Folder Name>").canWrite()) { 
      Toast.makeText(MainActivity.this, "Unable to write into external storage", Toast.LENGTH_SHORT).show(); 
     } 
     else{ 
      if(!currentDB.exists()){ 
       Toast.makeText(MainActivity.this, "Database doesn't exists", Toast.LENGTH_SHORT).show(); 
      } 
      else{ 
       try { 
        FileChannel src = new FileInputStream(currentDB).getChannel(); 
        FileChannel dst = new FileOutputStream(backupDb).getChannel(); 
        dst.transferFrom(src, 0, src.size()); 
        src.close(); 
        dst.close(); 
        MediaScannerConnection.scanFile(getApplicationContext(), new String[]{backupDb.toString()}, null, null); 
        editText.setText(null); 
        Toast.makeText(MainActivity.this, "Database successfully copied to external storage", Toast.LENGTH_SHORT).show(); 
       }catch(Exception e){ 
        Toast.makeText(MainActivity.this, "Got exception" + e, Toast.LENGTH_SHORT).show(); 
       } 
      } 
     } 
    } 
} 

“数据库名称” - >数据库内部应用程序
“备份数据库名称” - >文件中,你要复制的数据库
“文件夹名称” - >将备份数据库文件夹存储