How to access data/data folder in Android device?

后端 未结 18 1908
南方客
南方客 2020-11-22 15:36

I am developing an app and I know my database *.db will appear in data/data/com.****.***

I can access this file from AVD in Eclipse with he

18条回答
  •  太阳男子
    2020-11-22 16:23

    You can also try copying the file to the SD Card folder, which is a public folder, then you can copy the file to your PC where you can use sqlite to access it.

    Here is some code you can use to copy the file from data/data to a public storage folder:

    private void copyFile(final Context context) {
        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
    
            if (sd.canWrite()) {
                String currentDBPath =
                        context.getDatabasePath(DATABASE_NAME).getAbsolutePath();
    
                String backupDBPath = "data.db";
    
                File currentDB = new File(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) {
            e.printStackTrace();
        }
    }
    

    Manifest:

    
    
    
    
    

提交回复
热议问题