Last active
October 1, 2023 00:30
-
-
Save tkrajina/6e48840b3f9ecf15489c643718e2ce11 to your computer and use it in GitHub Desktop.
Copy android app database to sdcard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package info.puzz.a10000sentences.utils; | |
import android.content.Context; | |
import android.os.Environment; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.nio.channels.FileChannel; | |
/* | |
puzz ~/projects/10000sentences (master *=) $ adb pull /sdcard/debug_info.puzz.a10000sentences.sqlite | |
[100%] /sdcard/debug_info.puzz.a10000sentences.sqlite | |
puzz ~/projects/10000sentences (master *=) $ sqlite3 debug_info.puzz.a10000sentences.sqlite | |
SQLite version 3.14.0 2016-07-26 15:17:14 | |
Enter ".help" for usage hints. | |
sqlite> | |
*/ | |
public final class DebugUtils { | |
private DebugUtils() throws Exception { | |
throw new Exception(); | |
} | |
public static void backupDatabase(Context context, String databaseName) { | |
try { | |
File sd = Environment.getExternalStorageDirectory(); | |
File data = Environment.getDataDirectory(); | |
String packageName = context.getApplicationInfo().packageName; | |
if (sd.canWrite()) { | |
String currentDBPath = String.format("//data//%s//databases//%s", | |
packageName, databaseName); | |
String backupDBPath = String.format("debug_%s.sqlite", packageName); | |
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(); | |
} | |
} | |
} catch (Exception e) { | |
throw new Error(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can i import .db from storage to app?