2016-06-10 4 views
0

데이터베이스의 백업을 성공적으로 만들었지 만 내 백업이 SDcard가 아닌 내부 메모리에 저장된다는 문제가 있습니다. 컨텍스트 클래스/SQLiteOpenHelper 클래스는 당신이 다른 곳 데이터베이스 파일하지만 내부 응용 프로그램 스토리지 .IE /data/data/com.you를 저장하지 않는 기본적으로SD 카드에서 데이터베이스 백업하는 방법

public void exportDatabase() 
{ 
    File sd = Environment.getExternalStorageDirectory(); 
    File data = Environment.getDataDirectory(); 
    String currentDBPath = "/data/" + "com.arpanexample.spm" + "/databases/" + Database.DATABASE_NAME; 
    String backupDBPath = Database.DATABASE_NAME; 
    File currentDB = new File(data, currentDBPath); 
    File backupDB = new File(sd, backupDBPath); 
    try { 
     FileChannel source = new FileInputStream(currentDB).getChannel(); 
     FileChannel destination = new FileOutputStream(backupDB).getChannel(); 
     destination.transferFrom(source, 0, source.size()); 
     source.close(); 
     destination.close(); 
     Toast.makeText(context, "Successfully backup your data", Toast.LENGTH_LONG).show(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

답변

0

:

여기 내 코드입니다. 패키지 이름/데이터베이스. SQLite 데이터베이스를 다루기위한 커스텀 클래스를 얻거나 on-the-roll-out을 사용하십시오. 당신이하고있는 일을 안다면 안드로이드 소스 디렉토리에서 SQLiteOpenHelper 클래스를 가져 와서 편집하십시오. 여기에 하나의 정의가 있습니다 implementation of SQLiteOpenHelper you can use.

0

비슷한 것을 시도하고 싶었지만 적절한 튜토리얼을 찾지 못했습니다.

나중에 FileCache를 사용하여 SD 카드에 콘텐츠를 저장하십시오. 캐싱은 인터넷에 연결할 수있는 앱에 도움이됩니다.

package com.sumukh.myApp.anotherjsonparser; 

import java.io.File; 

import android.content.Context; 

public class FileCache { 

private File cacheDir; 

public FileCache(Context context) { 
    // Find the dir to save cached images 
    if (android.os.Environment.getExternalStorageState().equals(
      android.os.Environment.MEDIA_MOUNTED)) 
     cacheDir = new File(
       android.os.Environment.getExternalStorageDirectory(), 
       "AppCache"); 
    else 
     cacheDir = context.getCacheDir(); 
    if (!cacheDir.exists()) 
     cacheDir.mkdirs(); 
} 

public File getFile(String url) { 
    String filename = String.valueOf(url.hashCode()); 
    // String filename = URLEncoder.encode(url); 
    File f = new File(cacheDir, filename); 
    return f; 

} 

public void clear() { 
    File[] files = cacheDir.listFiles(); 
    if (files == null) 
     return; 
    for (File f : files) 
     f.delete(); 
    } 

} 

희망이 있습니다.

관련 문제