我正在开发一个应用程序,我知道我的数据库*.db将出现在data/data/com.****.***

我可以在sqlite管理器的帮助下从Eclipse中的AVD访问这个文件

但是我的安卓手机无法访问这个文件。 我谷歌了一下,它说我需要在我的手机上查找,但我不想这样做。

我如何访问我的data/data/.....目录在我的安卓手机“没有根它”?

是否可以更改目录data/data.....的用户权限没有生根?


当前回答

要做到以上任何一项(即从手机本身访问受保护的文件夹),你仍然需要root。(这包括更改/data文件夹的挂载权限并访问它)

如果没有root,除了通过代码从应用程序内部读取外,直接访问/数据是不可能的。所以你可以试着把那个文件复制到sdcard或者其他可以访问的地方,然后,你就可以正常访问它了。

如果您使用的是开发人员设备,那么root不会使您的保修失效。对不起,我没有别的办法了。

其他回答

其中一个简单的方法是在sd卡上创建数据库。因为你无法访问手机内存中的数据文件夹,除非你对手机进行root操作。那么为什么不简单地在sd卡上创建数据库呢?

此外,如果您愿意,可以编写一些文件复制代码,将现有的数据库文件(从内部内存)复制到外部内存,而不需要任何根。

您还可以尝试将文件复制到SD卡文件夹,这是一个公共文件夹,然后您可以将文件复制到您的PC上,在那里您可以使用sqlite访问它。

下面是一些你可以用来将文件从data/data复制到公共存储文件夹的代码:

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();
    }
}

清单:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />

直接在手机上访问这些文件很困难,但你可以将它们复制到你的电脑上,在那里你可以做任何你想做的事情。 没有根你有两个选择:

If the application is debuggable you can use the run-as command in adb shell adb shell run-as com.your.packagename cp /data/data/com.your.packagename/ Alternatively you can use Android's backup function. adb backup -noapk com.your.packagename You will now be prompted to 'unlock your device and confirm the backup operation'. It's best NOT to provide a password, otherwise it becomes more difficult to read the data. Just click on 'backup my data'. The resulting 'backup.ab' file on your computer contains all application data in android backup format. Basically it's a compressed tar file. This page explains how you can use OpenSSL's zlib command to uncompress it. You can use the adb restore backup.db command to restore the backup.

简单的答案是否定的。在即将到来的Android 13上,你不能访问任何东西 /存储/模拟/ 0 / Android / * 目录没有根你的设备或连接到PC,当然不是在Pixel设备。

阅读Android源页面,使用ADB访问这样的应用程序数据: https://developer.android.com/training/data-storage/manage-all-files

我也曾经遇到过同样的问题。除了adb shell或根设备,没有办法在android设备中直接访问文件。

下面是02种替代方案:

1)

 public void exportDatabse(String databaseName)
   {
     try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//"+getPackageName()+"//databases//"+databaseName+"";
            String backupDBPath = "backupname.db";
            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) {

    }
}

2)试试这个:https://github.com/sanathp/DatabaseManager_For_Android