393

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 help of sqlite manager

But I can't access this file in my Android phone.
I googled it and it says I need to root my phone to do it, but I don't want to do that.

How can I access my data/data/..... directory in my Android phone "without rooting it"?

Can I change user permissions for the directory data/data..... without rooting it?

phrogg
  • 888
  • 1
  • 13
  • 28
Naveen Prince P
  • 4,521
  • 3
  • 16
  • 17
  • 1
    do u have database.db file ..??? – Amit Prajapati Oct 22 '12 at 07:01
  • s i am using database - "openOrCreateDatabase(....)" – Naveen Prince P Oct 22 '12 at 16:36
  • 5
    You will definitely need to root the phone first of all. Then you will need a competent file manager app you can use. ASTRO file manager is popular but it has no root access support so it's useless for power users. You should be able to use ES File Explorer or File Expert. I have recently had problems viewing /data/data or other protected folders with ES File Explorer and have pretty much abandoned it, and I am now using File Expert. They both require that you go into settings and enable root explorer option (and sometimes also mount the file system as writable). So you need to do that first. – Samir Jul 05 '13 at 10:33
  • This is not programming related question nor answers. – SkyWalker Apr 03 '15 at 07:33
  • http://stackoverflow.com/a/18472135/1778421 – Alex P. Oct 03 '16 at 03:40
  • I posted [here](http://stackoverflow.com/a/43781842/3012993). You can go through this. – Satish Sojitra May 05 '17 at 04:49

18 Answers18

452

Accessing the files directly on your phone is difficult, but you may be able to copy them to your computer where you can do anything you want with it. Without rooting you have 2 options:

  1. 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/
    
  2. 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:

dd if=mybackup.ab bs=24 skip=1|openssl zlib -d > mybackup.tar

If the openssl zlib command does not work on your system, there are other options.

You can use the adb restore backup.db command to restore the backup.

flaviut
  • 2,007
  • 3
  • 23
  • 32
THelper
  • 15,333
  • 6
  • 64
  • 104
  • 23
    `adb backup -f \location\on\local\drive -noapk app.package.name` worked for me without root – theblang Dec 04 '13 at 16:28
  • 29
    Use this command to uncompress your backup file: `dd if=backup.ab bs=24 skip=1|openssl zlib -d > backup.tar` – Human Mar 17 '14 at 18:27
  • The 2nd option appears to not work on 4.4.2 Maybe because I did not develop the app? – Antony Apr 22 '14 at 23:56
  • @Antony Are you sure you replaced 'com.your.packagename' with the correct package name of the app you are trying to backup? – THelper Apr 23 '14 at 06:57
  • The issue actually has to do with on my end. I encrypted my phone, so I did not know I was supposed to use the same encryption key to backup. Problem solved. – Antony Apr 24 '14 at 14:09
  • This is actually a known bug with some system images on android 4.3+. To see if you are affected: 1. Fire up `adb shell` 2. `ls -l` on `data/system` 3. If the permissions on `packages.list` is `-rw-rw----`, you are affected – KyleCrowley Jul 20 '14 at 20:26
  • 14
    In Option 1, how do I tell the cp command to save them to my laptop (Mac)? If you give a Win example, I'd like to know too, and will just translate it. Thanks. – Gerard Sep 03 '14 at 04:20
  • 2
    *run-as com.your.packagename* command is working. says to me unknown package name – nAkhmedov Mar 03 '15 at 13:59
  • @nAkhmedov You have to replace `com.your.packagename` with the actual package name of the app you want to access. If you already did that check for typos. – THelper Mar 03 '15 at 14:08
  • 13
    **Important:** Running `adb backup -noapk com.your.packagename` will save the ab file on the *current directory*, so make sure you have navigated in the right directory (e.g. `cd Desktop..`) – Menelaos Kotsollaris May 20 '15 at 15:12
  • 1
    i am doing adb backup. but the problem is when i decompress the tar. i am getting an empty folder. no content is there :( – Abdul Mohsin Sep 21 '15 at 08:27
  • 2
    Once you have the backup file, you can use following on command line to unpack it, worked for me on mac, remove quotes "( printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" ; tail -c +25 backup.ab ) | tar xfvz -" – matangs Jan 11 '16 at 01:07
  • 1
    i dont understand what cp /data/data/com.your.pacakagename/ is doing? why are you copying something? – Lv99Zubat Jun 02 '16 at 18:27
  • 2
    With option 2 my backup has 41 bytes, after using Andorid Backup Extractor it produces broken tar. – Ursus Jun 11 '16 at 21:38
  • @THelper are you implying that I need to specify my target as the desktop of my computer or something (how would i do that)? Even doing that, I wouldn't expect that to do anything. In order to get files from internal storage on my computer, I had to jump through a few hoops. First I had to reference the files and make them "setReadable" in my code then I had to open another cmd prompt and use adb pull. Does using cp in a shell work for you? – Lv99Zubat Jun 14 '16 at 16:29
  • To extract the backup into a .tar file you can use the instructions from here: http://forum.xda-developers.com/showthread.php?t=2011811 – Matt__C Jul 27 '16 at 20:12
  • Also, make sure you have a newer adb.exe. I used an old adb and it only produced an empty backup file. – Dan Mirescu Jan 21 '18 at 21:07
  • It really worked for me. I was able to copy .apk file on non-rooted device. The only thing I had to find out is package name. Then I copied it on flash: $ adb shell shell@j7e3g:/ $ cp /data/app/com.nullsoft.winamp-1/base.apk /storage/9016-4EF8/_my Thanks! – Pavel Netesa Feb 24 '18 at 18:19
  • @THelper I think option 1 is not completely correct, you have cp to some other directory that is accessible from PC. What works for my is "cp /data/data/com.your.packagename/file.txt /sdcard/Download". Then exit run-as and shell and pull needed file to PC "pull /sdcard/Download/file.txt". – Tadas Šukys Jul 27 '18 at 16:06
  • 25
    Note that the backup approach will yield an empty backup if your app has disabled backups via `android:allowBackup="false"` in the manifest. – Tom May 01 '19 at 01:24
  • @Hooman - can you explain what "dd" is and where I can get it for Windows? – Dave Black Sep 09 '19 at 23:19
  • @DaveBlack, `dd` is a Unix tool for reading/writing from/to files or different input/output streams. I don't know of any counterpart in Windows! https://linux.die.net/man/1/dd – Human Sep 10 '19 at 18:28
  • 1
    Running the "adb backup" command deleted my Android Simulator from Android Studio! – Pete Alvin Dec 21 '19 at 16:59
  • To turn the backup.ab file into backup.tar, I installed "Android backup extractor" by nelenkov. I used it on Linux, but since it is Java I would expect it to run on Windows too. On the github page, I scrolled down to Releases and downloaded the abe.jar file. To run it, do something like this: `java -jar abe.jar unpack backup.ab backup.tar` – dan-gph Jan 24 '23 at 13:16
194

If you are using Android Studio 3.0 or later version then follow these steps.

  1. Click View > Tool Windows > Device File Explorer.
  2. Expand /data/data/[package-name] nodes.

You can only expand packages which runs in debug mode on non-rooted device.

Steps followed in Android Studio 3.0

Shahidul
  • 2,997
  • 1
  • 21
  • 20
62

You could also try fetching the db using root explorer app. And if that does not work then you can try this:

  1. Open cmd
  2. Change your directory and go into 'Platform tools'
  3. Type 'adb shell'
  4. su
  5. Press 'Allow' on device
  6. chmod 777 /data /data/data /data/data/com.application.package /data/data/com.application.package/*
  7. Open DDMS view in Eclipse and from there open 'FileExplorer' to get your desired file

After this you should be able to browse the files on the rooted device.

Sheraz Ahmad Khilji
  • 8,300
  • 9
  • 52
  • 84
25

To do any of the above (i.e. access protected folders from within your phone itself), you still need root. (That includes changing mount-permissions on the /data folder and accessing it)

Without root, accessing the /data directly to read except from within your application via code isn't possible. So you could try copying that file to sdcard or somewhere accessible, and then, you should be able to access it normally.

Rooting won't void your warranty if you have a developer device. I'm sorry, there isn't any other way AFAIK.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • i am not using an developers device. Mine is Xperia U – Naveen Prince P Oct 22 '12 at 16:29
  • can i edit user permission of data folder with chmode commands? – Naveen Prince P Oct 22 '12 at 16:37
  • The whole partition is mounted as read-only. I'd say your best bet would be `adb pull` as mentioned in the below answer. – Anirudh Ramanathan Oct 22 '12 at 16:39
  • I is possible if the application allows it. The directories all have x permission for everybody (but no r for everybody, so you have to know the path). Than it depends on whether the application made the file world accessible or not. Some files are, most are not. – Jan Hudec Oct 04 '13 at 09:08
  • First you say it's impossible to read, then you suggest to copy the file elsewhere. But, how to copy a file without reading it in the first place? I think this answer's buggy. – untill Jun 10 '15 at 12:57
  • @until, your application can still access the fie. You can write code to copy that file out to a universally accessible location. Incidentally, that's what the accepted answer also suggests, but by a different mechanism. – Anirudh Ramanathan Jun 10 '15 at 15:34
  • Gonna have to return the (verizon) pixel 2 I was just given for xmas, then... and pay $300 more out of pocket for a rootable one – Wyatt Ward Dec 25 '17 at 21:38
  • I think you need to modify your answer. By European laws you may root your device without losing your warranty unless phone manufacturer will prove that your direct actions led to software or hardware malfunctions. Most of the companies like Xiaomi or Samsung do not void your warranty if you (only) root your device (in european markets). – Andrey Borisovich Jun 08 '21 at 16:36
15
  1. Open your command prompt
  2. Change directory to E:\Android\adt-bundle-windows-x86_64-20140702\adt-bundle-windows-x86_64-20140702\sdk\platform-tools
  3. Enter below commands
  4. adb -d shell
  5. run-as com.your.packagename cat databases/database.db > /sdcard/database.db
  6. Change directory to cd /sdcard to make sure database.db is there.
  7. adb pull /sdcard/database.db or simply you can copy database.db from device .
Leos Literak
  • 8,805
  • 19
  • 81
  • 156
15

The easiest way (just one simple step) to pull a file from your debuggable application folder (let's say /data/data/package.name/databases/file) on an unrooted Android 5.0+ device is by using this command:

adb exec-out run-as package.name cat databases/file > file
Alex P.
  • 30,437
  • 17
  • 118
  • 169
10

Use File Explorer in eclipse. Select Windows => Show View => Other ... => File Explorer.

An another way is pull the file via adb:

adb pull /system/data/data/<yourpackagename>/databases/<databasename> /sdcard
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165
7

To backup from Android to Desktop

Open command line cmd and run this:
adb backup -f C:\Intel\xxx.ab -noapk your.app.package.
Do not enter password and click on Backup my data.
Make sure not to save on drive C root. You may be denied.
This is why I saved on C:\Intel.

To extract the *.ab file

  1. Go here and download: https://sourceforge.net/projects/adbextractor/
  2. Extract the downloaded file and navigate to folder where you extracted.
  3. run this with your own file names:
    java -jar abe.jar unpack c:\Intel\xxx.ab c:\Intel\xxx.tar
greybeard
  • 2,249
  • 8
  • 30
  • 66
RonTLV
  • 2,376
  • 2
  • 24
  • 38
  • 1
    This is a great answer, thank you. Worked for me for extracting a single-app backup created on a Xiaomi device (with .bak extension). No rooting required – Russian Jun 05 '18 at 23:16
4

may be to access this folder you need administrative rights.

so you have two options:-

  1. root your device and than try to access this folder
  2. use emulator

p.s. : if you are using any of above two options you can access this folder by following these steps

open DDMS perspective -> your device ->(Select File Explorer from right window options) select package -> data -> data -> package name ->files

and from there you can pull up your file

Jayanth
  • 5,954
  • 3
  • 21
  • 38
4

I had also the same problem once. There is no way to access directly the file within android devices except adb shell or rooting device.

Beside here are 02 alternatives:

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) Try this: https://github.com/sanathp/DatabaseManager_For_Android

Khairul Alam Licon
  • 420
  • 10
  • 19
4

On a rooted device, the correct solution is this:

Open cmd
Change your directory and go into 'Platform tools'
Type 'adb shell'
su
Press 'Allow' on device
chmod 777 /data /data/data /data/data/*
Open DDMS view in Eclipse/IntelliJ and from there open 'FileExplorer' to get your desired file

The original solution worked, but the chmod would return unknown directory. Changing the chmod command to /data/data/* gave access to all subfolders in the data directory from DDMS in Intellij. I assume the same solution is true for Eclipse DDMS.

UPDATE So, what I've found is strange. I'm running a Nexus 6 using DDMS in IntelliJ (Android Device Monitor). I have built a little starter app. Said app saves data to a .csv file in data/data/com.example.myapp/files

When I first started to try to access this file on my Nexus 6, I found that I have to root the device.. I could see the data folder, but trying to open it would not work. As mentioned online in other places, the expand + would vanish then reappear shortly thereafter (note, there are solutions on the web that claim to allow access to these folders without rooting, I didn't find them till too late, and I'm not sure if I prefer not to root anyway ((I'd rather be able to do it manually than rely on an app or command prompt to give me my solutions))). I rooted my 6 and tried DDMS again.

At this point, it showed me the data folder and I could expand the folder and see the com. directories, but I could not open any of them. That is when I discovered the above solution. The initial instructions would not work on this part:

chmod 777 /data /data/data /data/data/com.application.pacakage /data/data/com.application.pacakage/*

That is when I tried the solution I posted:

chmod 777 /data /data/data /data/data/*

That solution seemed to work, but only on certain folders. I now could expand my myapp folder, but could not expand the files directory in it.

At this point, I played around for a while then figured why not just try it on the directory I need rather than trying these wildcard entries.

chmod 777 /data /data/data /data/data/com.example.myapp/*

Followed by:

chmod 777 /data /data/data /data/data/com.example.myapp/files

These commands allowed me to expand and view the files in my app's directory to confirm that the .csv was being saved correctly.

Hope this helps someone. I struggled with this for hours!

(to compound on this a tad further, oddly enough, the permissions did not pass to the .csv file that passed to the files directory. my files directory permissions read drwxrwxrwx and my log.csv file permissions read -rw-rw---- .. just fyi)

lol2dubs
  • 67
  • 1
  • 8
4

You can download a sigle file like that:

adb exec-out run-as debuggable.app.package.name cat files/file.mp4 > file.mp4

Before you download you might wan't to have a look at the file structure in your App-Directory. For this do the following steps THelper noticed above:

adb shell
run-as com.your.packagename 
cd files
ls -als .

The Android-Studio way Shahidul mentioned (https://stackoverflow.com/a/44089388/1256697) also work. For those who don't see the DeviceFile Explorer Option in the Menu: Be sure, to open the /android-Directory in Android Studio. E.g. react-native users have this inside of their Project-Folder right on the same Level as the /ios-Directory.

suther
  • 12,600
  • 4
  • 62
  • 99
3

adb backup didn't work for me, so here's what I did (Xiaomi Redmi Note 4X, Android 6.0):
1. Go to Settings > Additional Settings > Backup & reset > Local backups.
2. Tap 'Back up' on the bottom of the screen.
3. Uncheck 'System' and 'Apps' checkmarks.
4. Tap detail disclosure button on the right of the 'Apps' cell to navigate to app selection screen.
5. Select the desired app and tap OK.
6. After the backup was completed, the actual file need to be located somehow. Mine could be found at /MIUI/backup/AllBackup/_FOLDER_NAMED_AFTER_BACKUP_CREATION_DATE_.
7. Then I followed the steps from this answer by RonTLV to actually convert the backup file (.bak in my case) to tar (duplicating from the original answer):
"
a) Go here and download: https://sourceforge.net/projects/adbextractor/
b) Extract the downloaded file and navigate to folder where you extracted.
c) run this with your own file names: java -jar abe.jar unpack c:\Intel\xxx.ab c:\Intel\xxx.tar
"

Russian
  • 1,296
  • 10
  • 15
2

Simple answer is NO. On upcoming Android 13, you can't access anything in /storage/emulated/0/Android/* directory without Rooting your device or hooking up to a PC, certainly not in Pixel devices.

Read Android Source page for such App data access using ADB here: https://developer.android.com/training/data-storage/manage-all-files

Nirmal
  • 57
  • 1
  • 8
1

One of the simple way is to create your database on SD-Card. Because you cannot get access to your phone's data folder in internal memory, unless you root your phone. So why not simply create your database on SD-Card.

Moreover, if you want, you may write some file copying-code to copy your existing database file (from internal memory) to external memory without requiring any root.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • how to do it? how can i create a db in sdcard – Naveen Prince P Oct 22 '12 at 16:41
  • [This](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#openOrCreateDatabase) method will help you to create database on sd-card. Simply provide a path of `File` pointing to SD-Card and you are good to go – waqaslam Oct 22 '12 at 21:22
0

you can copy this db file to somewhere in eclipse explorer (eg:sdcard or PC),and you can use sqlite to access and update this db file .

Vito
  • 1,080
  • 9
  • 19
0

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:

<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" />
live-love
  • 48,840
  • 22
  • 240
  • 204
0

SQLlite database is store on user's Phone and it's hidding under path:

/Data/Data/com.companyname.AppName/File/

you have 2 options here:

  1. you can root your phone so that you get access to view your hidding db3 file
  2. this is not a solution but a work around. Why not just create test page that display your database table in it using 'select' statment.