How do I view Android application specific cache? - android

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?

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.

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

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)

You can check the application-specific data in your emulator as follows,
Run adb shell in cmd
Go to /data/data/ and navigate into your application
There you can find the cache data and databases specific to your application

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

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.

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

Related

ADB push folder from Windows to internal app storage

I'm trying to move all sub-folder of a folder on my desktop to the 'files' folder in the internal app storage of the app I developed.
My first attempt with one file:
/e/Android/android-sdk/platform-tools
$ adb push pull.sh /data/data/irisrecognition.example.com.irisrecognition
failed to copy 'pull.sh' to 'C:/Program Files/Git/data/data/irisrecognition.example.com.irisrecognition': No such file or directory
26 KB/s (315 bytes in 0.011s)
Why does the GIT path gets added to my data path? I also tried using adb shell, run-as etc to no avail.
Here is what worked for our App:
Ensure the device is connected.
Open a console.
Finally, run something like:
adb push C:\my-location\my-data\. /storage/emulated/0/Android/data/my.package.name
But edit above's paths to your requirements (specially the "my.package.name" part).
Note that the "." dot means current-directory, but by putting "C:\my-location\data\" in front of it, we mark that path to be current.
Also, the destination-directrory is auto created (if it does not already exist).
You can't simply push or pull files into or from the internal storage of an app. If it would have been possible, any 3rd party app could fetch or inject data into an app's private storage space.
If you really want to do this, you need to have root access.

How can a directory which is created via adb shell be executable in Android app

I has made a directory named abc via adb shell in /data/data/com.helloworld.new_munion_sdk_demo/files/ and com.helloworld.new_munion_sdk_demo is my app name.
I try to visit the files in abc in my app and I found no file can be read with a FileNotFoundException.
Because the dir made by shell is default belongs to user root, so I use chown and chmod 700 to change its permission as follow:
enter image description here
The dir abc is seems just no different like other normal directories, but its very odd this dir is can not be executable in the app, thus files in dir abc can not be readable.
enter image description here
This snapshot is from Android Studio.
I'm using nexus 5 and rom 6.0.1 complied by aosp.
If you want to use a directory to store files, you might want to look into this article on storage for android
Android Developers: Saving Files
Your app has access to this folder in Internal storage, which will be created using your own code.
The partition /data is reserved for the OS as far as I know.

Getting SQLite database from emulator (data/data/package_name/databases) to local drive

I would like to edit my app's DB using the SQLite Browser. I'm able to edit the DB using the adb shell (sqlite3), but I would rather edit it using a GUI rather than a command line. How do I get the DB from the emulator to a local drive? As of now I've tried:
1) using the adb pull command to pull the database from the emulator to my local drive.
adb pull data/data/com.myapps.quiz c:/
This command executes correctly, but I'm not able to find the file or directory in the local drive I specified.
2) Used the DDMS Perspective to locate the file in the File Explorer, but every time I get into the data/data directory, I only see directories called "con". I even tried pulling the entire data/data directory, but I can't find the name of the package (com.myapps.quiz) where the database is stored.
What am I missing here? Any help you could provide would be most helpful!
Your database is secure to your package so you cannot get this directly.
I have a work around for this.
What I do is go to shell and run-as my package and copy the database to sdcard using cat
adb shell
run-as your.package.name
cd databases
cat your_database.db > /mnt/sdcard/your_database.db
And I pull the file from the sdcard using File Explorer
Are you sure you're connecting to the emulator instance in DDMS, and not an actual device? I have always been able to push and pull sqlite databases from the
data/data/com.my.package/databases/mydb.sqlite
directory, and I can't think of any setting that would override that. An alternative would be to root an actual phone and then you will be able to move anything from the phone to your computer.

Viewing private files created by an android app

I have an xml file being written by an app that is set to MODE_PRIVATE, but I now want to read that file outside of the phone, for debugging purposes. In Eclipse, I can access other files made by the app and copy them to my computer, but I can't even see this private file. Merely changing the file to MODE_WORLD_READABLE file doesn't seem to help. I think the file is being stored on an internal "SD card" that can not be removed from the phone, but there are also two other folders in the File Explorer that are either empty or inaccessible: asec and secure.
Does anyone know how the file can be accessed?
If your app is installed in debug mode, you can get your private files on a device without rooting.
Go to [android-sdk]/platform-tools folder and run adb shell.
run-as com.example.yourapp
cp -r /data/data/com.example.yourapp /sdcard/
(Where com.example.yourapp is the package name of your application.)
After executing the steps above, the private folder of your application is copied into the root of your sdcard storage, under your package name, where you have permission to download and view them.
Note 1: If you don't need to download them, then instead of step 3, you can use unix commands to navigate around and list files and folders.
Note 2: Starting from Android Studio 2.0, you'll find more files in the cache and files/instant-run folder, related to the Instant Run and the GPU Debugger features, placed there by the IDE.
You will need to connect the phone and do some magic to let your sdk work with it (I think put it in debugging mode?). Go to where you unzipped the android sdk:
C:\android-sdk_r10-windows\android-sdk-windows\platform-tools>adb shell
#cd data/data/com.yourpackage.yourapp/files
#ls
You should see your file listed. You may need to run "ls data/data" if you're not sure what the fully-qualified name of your app is. From here if the file is small and you just want to see what's inside it you can run:
#cat yourfilename.xml
Alternatively:
#exit
C:\android-sdk_r10-windows\android-sdk-windows\platform-tools>adb pull /data/data/com.yourpackage.yourapp/files/yourfile.xml
Note: I have only tried this on the emulator, I don't know how to use adb with a physical phone.
You need to root your phone to see Context.MODE_PRIVATE files
It ends up being stored in data//files I believe but you need root permission to see them
So either root your phone or wait until you finished debugging and then add Context.MODE_PRIVATE
If Eclipse is used, there is one more option:
DDMS Perspective > File Explorer tab > data/data/com.yourpackage.yourapp/files
where you can pull/push/delete files.
Another option is to have a command in the app that dumps the private files. This only works if you don't want to edit the files, but has the added bonus that you don't have to strip it out before it goes to production, because the user can't break anything with it. Well, as long as the files don't contain sensitive information. But, really, if they do, you're doing something wrong. As #user1778055 said, a user can root their phone to access it.

Why can't 'data' folder be displayed just like in DDMS file explorer?

I list out files in '/' by:
File directory = new File("/");
fill_listview(directory.listFiles());
And I get those in list:
sqlite_stmt_journals
config
cache
sdcard
d
etc
system
sys
sbin
proc
init.rc
init.goldfish.rc
init
default.prop
data
root
dev
I have two questions:
1.Why cann't I access '/data' folder just like Eclipse DDMS file explorer?
2.Why it is different from the DDMS which show only:
data
sdcard
system
Thanks!
On rooted device you can do this to access the /data folder:
Open cmd
Type 'adb shell'
su
Press 'Allow' on device
chmod 777 /data /data/data /data/data/com.application.pacakage
Go to the DDMS view in Eclipse
After this you should be able to browse the files on the device.
It is mostly because of security issues. If /data folder is visible to everyone, then some malicious app can read/temper/delete the data of some other app which can get really worse if some app is storing some sensitive data like password/credit card number etc. So the whole filesystem is only visible via debug console (or ddms).You can not access data of any app on the device unless it is rooted because by doing so, integrity of system might be compromised and it may lead of weird behavior. Which means you can not access cached data/databases of app.
I did this on a non-rooted device and it worked
run-as com.your.package ls -l /data/data/com.your.package
run-as com.your.package rm /data/data/com.your.package/databases/mydatabase.db
Reference: http://denniskubes.com/2012/09/25/read-android-data-folder-without-rooting/

Categories

Resources