42

Is there any way to dynamically view the application specific cache in Android? I'm saving images to the cache (/data/data/my_app_package/cache) and I'm 99% sure they're saving there, but not sure how long they're staying around.

When I look in the cache using the DDMS File Explorer within Eclipse, it's always empty. I've also tried examining the appropriate cache dir in ADB and again it's always empty.

Any suggestions?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Karim Varela
  • 7,562
  • 10
  • 53
  • 78

7 Answers7

40

You may use this command for listing the files for your own debuggable apk:

adb shell run-as com.corp.appName ls /data/data/com.corp.appName/cache

And this script for pulling from cache:

#!/bin/sh
adb shell "run-as com.corp.appName cat '/data/data/com.corp.appNamepp/$1' > '/sdcard/$1'"
adb pull "/sdcard/$1"
adb shell "rm '/sdcard/$1'"

Then you can pull a file from cache like this:

./pull.sh cache/someCachedData.txt

Root is not required.

Tamas
  • 3,254
  • 4
  • 29
  • 51
  • 1
    Yes, run-as can be useful but it only works with a debug apk. Also the tool is entirely broken on at least one Android version. – Chris Stratton Jul 10 '14 at 14:35
  • 1
    I prefer this shell script variation, which lets me pull from any nested dir and dump to a different output filename. Thanks! `adb shell "run-as $1 cat '/data/data/$1/$2' > '/sdcard/$3'"; adb pull "/sdcard/$3"; adb shell "rm '/sdcard/$3'"` – qix Apr 02 '15 at 03:42
  • 1
    nice script but why `cat` the file rather than simply copying it via `cp`? `cat` can sometimes screw up newlines so it seems like `cp` would be safer. – gMale Dec 20 '16 at 06:45
  • 2
    How can I do this with a non debuggable apk? – rollsch Oct 08 '17 at 02:59
  • I am seeing files with .exo file extension in a media folder for some app. Are those cryptic image files? Opening exo with image viewer doesn't open an image. – Rudolph May 14 '23 at 22:06
29

On Android Studio you can use Device File Explorer to view /data/data/your_app_package/cache.

Click View > Tool Windows > Device File Explorer or click the Device File Explorer button in the tool window bar to open the Device File Explorer.

Documentation

Darush
  • 11,403
  • 9
  • 62
  • 60
16

Unless ADB is running as root (as it would on an emulator) you cannot generally view anything under /data unless an application which owns it has made it world readable. Further, you cannot browse the directory structure - you can only list files once you get to a directory where you have access, by explicitly entering its path.

Broadly speaking you have five options:

  • Do the investigation within the owning app

  • Mark the files in question as public, and use something (adb shell or adb pull) where you can enter a full path name, instead of trying to browse the tree

  • Have the owning app copy the entire directory to the SD card

  • Use an emulator or rooted device where adb (and thus the ddms browser's access) can run as root (or use a root file explorer or a rooted device)

  • use adb and the run-as tool with a debuggable apk to get a command line shell running as the app's user id. For those familiar with the unix command line, this can be the most effective (though the toolbox sh on android is limited, and uses its tiny vocabulary of error messages in misleading ways)

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • This is a wrong answer, as you can access your own app's data through `adb` without root privileges, i.e. also from real unlocked devices. See my answer. – Tamas Jul 10 '14 at 13:14
  • 5
    @Tamas - no, this is a correct answer, and **already mentions** the possibility of the run as tool, including it's **limitaition** of requiring a debug apk, something you neglected to mention. – Chris Stratton Jul 10 '14 at 14:34
5

You can check the application-specific data in your emulator as follows,

  1. Run adb shell in cmd
  2. Go to /data/data/ and navigate into your application

There you can find the cache data and databases specific to your application

Hasangi
  • 280
  • 7
  • 17
3

Question: Where is application-specific cache located on Android?

Answer: /data/data

df3ct
  • 39
  • 1
2

Cached files are indeed stored in /data/data/my_app_package/cache

Make sure to store the files using the following method:

String cacheDir = context.getCacheDir();
File imageFile = new File(cacheDir, "image1.jpg");
FileOutputStream out = new FileOutputStream(imageFile);
out.write(imagebuffer, 0, imagebufferlength);

where imagebuffer[] contains image data in byte format and imagebufferlength is the length of the content to be written to the FileOutputStream.

Now, you may look at DDMS File Explorer or do an "adb shell" and cd to /data/data/my_app_package/cache and do an "ls". You will find the image files you have stored through code in this directory.

Moreover, from Android documentation:

If you'd like to cache some data, rather than store it persistently, you should use getCacheDir() to open a File that represents the internal directory where your application should save temporary cache files.

When the device is low on internal storage space, Android may delete these cache files to recover space. However, you should not rely on the system to clean up these files for you. You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your application, these files are removed.

sultan.of.swing
  • 1,090
  • 1
  • 12
  • 23
0

Here is the code: replace package_name by your specific package name.

Intent i = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:package_name"));
startActivity(i);
j0k
  • 22,600
  • 28
  • 79
  • 90