2017-06-29 10 views
-1

データベースをファイルにエクスポートします。ここに私のコードです。Android携帯からデータベースをエクスポートできません

public static void backupDatabase(Context mContext) throws IOException { 
    //Open your local db as the input stream 
    String DB_PATH=null; 
    if(android.os.Build.VERSION.SDK_INT >= 17) { 
     DB_PATH = mContext.getApplicationInfo().dataDir + "/databases/"; 
    } else { 
     DB_PATH = "/data/data/" + mContext.getPackageName() + "/databases/"; 
    } 
    DB_PATH=DB_PATH+"SamsungLightingApp.db"; 
    File dbFile = new File(DB_PATH); 
    FileInputStream fis = new FileInputStream(dbFile); 

    String outFileName = Environment.getExternalStorageDirectory() + "/MY DB.db"; 
    //Open the empty db as the output stream 
    OutputStream output = new FileOutputStream(outFileName); 
    //transfer bytes from the inputfile to the outputfile 
    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = fis.read(buffer)) > 0) { 
     output.write(buffer, 0, length); 
    } 
    //Close the streams 
    output.flush(); 
    output.close(); 
    fis.close(); 
} 

我々はそれがファイルが存在していないというエラーがスローされますFileInputStreamを開きます。 いつも私はFileNotFoundExceptionを得ています。

答えて

0

context.getDatabasePath(dbfilename)を使用して、データベースのファイルを取得します。決してハードコーディングされたパスを仮定しないでください。

+0

私はすでにそれをしましたが、運はありません – Godwin

0

context.getDatabasePath("dbfilename.db")はAPI 1以降です。データベースファイルが存在する場合は、確実に動作します。ファイルを返します。

1

あなたのデータベースがどこにあるかを確認するには、adbコマンドを実行してください。

adbのシェル

のrun-asパッケージ名

データベースのパスとチェックを見つけ、それをプログラムからあなたのパスに一致しています。

関連する問題