2012-01-30 2 views
1

내 내부 저장소에서 기존 저장소로 기존 데이터베이스를 복사하려고하는데 약간 문제가 있습니다. 그것은 그 the file does not exist 말한다.내부 저장소에서 외부 저장소로 안드로이드 복사 데이터베이스

외부 저장 장치 내부에서 파일을 복사 : 여기에 내가 실제로 사용하고 무엇

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/"에 대상 폴더로 설정하면 파일 Data.Example에 생성됩니다.

내가 누락 된 제안이 있으십니까?

답변

0

WRITE_EXTERNAL_STORAGE 권한을 추가 했습니까?

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

예, 이미 해 보았습니다. 실제로 파일을 복사 한 것으로 나타 났지만 비어 있습니다. –

5

데이터베이스를 sdcard로 복사 할 때 사용하십시오.

//for without "Resource leak: '<unassigned Closeable value>' is never closed" warning, something like this 
public void copyAppDbToDownloadFolder() throws IOException { 
    try { 
     String currDate = Constants.APP_DATE_SHORT_FORMAT.format(new Date()); 
     File backupDB = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), DbContract.DATABASE_BUCKUP_NAME + currDate + ".db"); 
     File currentDB = appCon.getDatabasePath(DbContract.DATABASE_NAME); 
     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; 
     } 
    } catch (IOException e) { 
     Log.d("Copying Database", "fail, reason:", e); 
    } 
} 

또는 공용 "다운로드"에 데이터베이스를 복사해야하는 경우 폴더 당신이 사용할 수 있습니다 :

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

+0

"if (bool == true)"? 나는 어딘가에 누락 된 초기화가 있다고 생각한다.가장 좋은 추측은 원본 db의 크기에 대해 "transferFrom"긴 응답을 확인하거나 1보다 큰지 확인하십시오 : https://developer.android.com/reference/java/nio/channels/FileChannel .html # transferFrom (java.nio.channels.ReadableByteChannel, long, long) –

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

이 프로그램은 안드로이드 애플 리케이션의 내부 스토리지에서 외부 스토리지로 "데이터베이스 이름"이라는 데이터베이스를 복사 할 수 있습니다. "폴더 이름"라는 폴더를 생성하고 "데이터베이스 이름 백업" 그 폴더 안에

1.이 프로그램은 "즉시 시스템
3.If는 다시 시작하지 않고 창은 운영 체제와 컴퓨터에서 백업 파일을 복사 할 수
2.As MediaScannerConnection를 사용하는 안드로이드 마시 멜로와 호환 폴더 이름 "이 존재하지 않으면 자동으로 생성됩니다
4. 지정한 백업 파일 이름이 > 응용 프로그램
"백업 데이터베이스 이름"내부 데이터베이스 - - 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(); 
       } 
      } 
     } 
    } 
} 

"데이터베이스 이름"이 표시됩니다 존재> 데이터베이스를 복사 할 파일있는
"폴더 이름"-> 백업 데이터베이스를 저장할 폴더

관련 문제