I almost feel embarrased asking this but I really can't find my app on my phone. I read that the app should be installed in /data/data/ folder but my /data folder appears to be empty when viewed in Astro. My app is most definitely installed on the phone, should I transfer it to the SD for it to become visible? I have an unrooted HTC Desire HD on Orange UK. I just need to have a peek at the SQLite database managed by my App.
I almost feel embarrased asking this but I really can't find my app on my phone.
Look for it in Settings > Applications to see if it is installed. If it is, any activities you declared in the LAUNCHER category (action MAIN) will appear in the home screen launcher.
I read that the app should be installed in /data/data/ folder but my /data folder appears to be empty when viewed in Astro.
You do not have permissions to view that directory on an un-rooted phone.
My app is most definitely installed on the phone, should I transfer it to the SD for it to become visible?
Your app will not be any more "visible".
I just need to have a peek at the SQLite database managed by my App.
Add a backup/restore feature to your app that copies your SQLite file to/from external storage. Be sure all your SQLiteDatabase objects are closed first, though.
Simple answer would be,
If you need to browse the "Data" directory in actual phone, it should be a rooted device.
You can simply browse "Data" directory in simulators, because, Simulator act as a rooted device and it has all the super user access.
Happy coding!!!
Update
There is another way by copying database to your SD card from below function,
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) {
}
}
root is not necessary if your application is debuggable (android:debuggable="true") see How can I see the contents of a sqlite db of my app in a real device?
Related
Android 4.4.4
Writing a simple app to learn basics of Android programming.
Running a debug build (on my device not in emulator), both in USB debug from Android Studio, and also on the device after disconnecting from USB.
I simply want to write text data to a text file on external storage. Don't care at this stage if it is internal storage or SD card (there is an SD card installed).
I HAVE got WRITE_EXTERNAL_STORAGE permission set in my manifest.
I AM checking that external storage is mounted - it is.
I have tried the code I found here:
File file = new File(myContext.getExternalFilesDir(null), "state.txt");
try {
FileOutputStream os = new FileOutputStream(file, true);
OutputStreamWriter out = new OutputStreamWriter(os);
out.write(data);
out.close();
}
I log filename to LOGCAT and it appears as
/storage/emulated/0/Android/data/com.amazed.matthew.brunel/files/file.csv
which looks reasonable. There is no exception thrown, code runs fine, just no file created!! So, is a file being created somewhere else because I'm in debug, or what? Am I missing something?
Thanks.
Following this example from Android Documentation you can do:
void write() {
File file = new File(getExternalFilesDir(null), "state.txt");
try {
OutputStream os = new FileOutputStream(file);
os.write(data);
} catch (IOException e) {
Log.w("ExternalStorage", "Error writing " + file, e);
} finally {
os.close();
}
}
Without using OutputStreamWriter, also keep in mind that according to documentation:
Starting in KITKAT, no permissions are required to read or write to
the returned path; it's always accessible to the calling app. This
only applies to paths generated for package name of the calling
application. To access paths belonging to other packages,
WRITE_EXTERNAL_STORAGE and/or READ_EXTERNAL_STORAGE are required.
Ok, I don't fully understand why behaviour is as described below, but basic issue is solved.
I powered down the device and rebooted - next time I connected the USB cable and looked in Windows Explorer on my host debug machine....there were my files. So I don't know why I can't see them without power cycling the device, but they are there. Weird!!
I want my Android application to create a public folder in the internal storage of the phone. I want to save some text files there that I want to be available even after the application uninstalls.
The goal is to create some txt that I want to transfer to my PC for further analysis.
I wanted to try the Environment.DIRECTORY_DOCUMENTS, but the targeted phone is old and it doesn’t have the folder.
I have read the documentation but I can’t find any solution. I also tried to hard code the folder, but still it wont be created. I just want to have a folder like the picture camera folder.
I have tried this but it doesn't work.
Here is the code that I use:
File file = new File("/folder to create here", "name of file here" + File.pathSeparator + "txt");
if (!file.mkdirs())
Log.e(getClass().toString(), "Save Data -> Directoy not created");
try {
fos = new FileOutputStream(file);
fos.write(saveString.toString().getBytes());
fos.flush();
fos.close();
}
catch (Exception e){
if (e instanceof FileNotFoundException) {
Toast.makeText(getApplicationContext(), "Could not create file, please try again", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
if (e instanceof IOException) {
Toast.makeText(getApplicationContext(), "Could not write in file, please try again", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
Is there any way to do it or not. There are many application like viber that create folders in the root.
Thanks in advance for your help.
As far as I know, there is no such mechanism to create a persist world writable directory. App's data directory in internal storage will be deleted after uninstall.
Since your device is connected with PC (Am I right?), here is my suggestion.
When your device is connected to your PC, in your PC, use adb tool to create a public writable directory under /data/local/tmp
adb shell mkdir /data/local/tmp/public_write
adb shell chmod 777 /data/local/tmp/public_write
And in your app, write the data you want to save to /data/local/tmp/public_wirte/data.txt
In you PC, you can use adb pull to access the file as long as your device is connected with your PC and the USB debug of your device is enable.
adb pull /data/local/tmp/public_write/data.txt
I'm not sure what the source of the problem is - older android version with bug or if I'm doing something wrong, but my problem is that no matter what I do, android reports the SD card as mounted. Even if it's not physically in the tablet (archos 7o)..
public boolean saveToDisk(String filename, String header) {
/* first check to see if the SD card is mounted */
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//throw some exception so we can display an error message
// XXX
return false;
}
try {
File root = Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/bioz");
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, filename);
....
The first test is always true, getExternalStorageDirectory() responds with /mnt/storage and the test to see if /mnt/storage/bioz exists indicates that the directory does exist.
Any idea what's going on? Am I doing something wrong, is the API broken, or something else?
Thanks,
Reza
External storage is not the same as SD card, at least not on all devices. Devices that have internal flash memory (for example my Nexus S does) threat this as " external storage".
Now, devices that have both internal flash and SD card, threat internal flash as external memory and SD card is then added as directory under this external memory.
From programmers view it's a pain, but not much we can do about it.
In my app, I store the user's app data using MySQLite databases, and I allow the user to backup the app data to the SD card within a folder that I create on the SD card (let's call it MyAppFolder). On Android devices that have only a single SD card slot, everything works fine (e.g. my Droid).
However, on devices such as the Galaxy S that have more than 1 SD card, things don't work. Unfortunately, I don't actually have one of these devices, so I can't debug anything, I can only go by user reports. I also did some searching and found this is a known issue. However, I did not find any solutions that didn't involve just hardcoding the other paths that get used, so I'm looking for some help with that.
In my app, I check and see if MyAppFolder already exists. If not, I create that folder. The folder is always created successfully, although it is created on the "internal" memory slot returned by getExternalStorageDirectory() when there are 2 slots present. However, the files do not get created and copied there. I don't understand why the folder is created, but the files are not created.
Can someone tell me how I can modify this code to work for devices with 2 card slots as well as 1 card slot? I'd prefer not to hard-code locations to check, but if that's the only way, I'll do it just to get things working.
Here is the code I use(slightly modified to make more readable here):
File root = Environment.getExternalStorageDirectory();
String state = Environment.getExternalStorageState();
if( Environment.MEDIA_MOUNTED.equals(state))
{
File dataDirectory = Environment.getDataDirectory();
if (root.canWrite())
{
String savePath = root + "/MyAppFolder/";
File directory = new File(savePath);
if( !directory.exists() )
{
directory.mkdirs(); //folder created successfully
}
String currentDBPath = "\\data\\my_app\\databases\\database.db";
File currentDB = new File(datDirectory, currentDBPath);
File backupDB = new File(savePath, "database.db");
if (currentDB.exists())
{
FileChannel src = new FileInputStream(currentDB[i]).getChannel();
FileChannel dst = new FileOutputStream(backupDB[i]).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
Can someone tell me how I can modify this code to work for devices with 2 card slots as well as 1 card slot?
"External storage" does not mean "removable storage". "External storage" means "mountable storage" -- IOW, the user has access to that storage when they plug their device into a host machine via a USB cable.
Android, at present, is designed to allow developers to write things to one external storage point, and it is up to the device manufacturer whether that is fixed flash or something removable. Hence, you should be backing things up to external storage, not thinking that you are backing things up to an SD card.
Can someone tell me how I can modify this code to work for devices with 2 card slots as well as 1 card slot?
Use getDatabasePath() to get a database path, rather than the gyrations you are presently going through. Never use concatenation to create paths, the way you are with root + "/MyAppFolder/" -- use a File constructor, as you are elsewhere. Make sure you hold the WRITE_EXTERNAL_STORAGE permission. Beyond that, this should work fine for any device with sufficient external storage to hold your database, regardless of how many "card slots" the device may have.
Sorry for the ambiguous title but I'm doing the following to write a simple string to a file:
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
System.out.println("Can write.");
File def_file = new File(root, "default.txt");
FileWriter fw = new FileWriter(def_file);
BufferedWriter out = new BufferedWriter(fw);
String defbuf = "default";
out.write(defbuf);
out.flush();
out.close();
}
else
System.out.println("Can't write.");
}catch (IOException e) {
e.printStackTrace();
}
But root.canWrite() seems to be returning false everytime. I am not running this off of an emulator, I have my android Eris plugged into my computer via USB and running the app off of my phone via Eclipse. Is there a way of giving my app permission so this doesn't happen?
Also, this code seems to be create the file default.txt but what if it already exists, will it ignore the creation and just open it to write or do I have to catch something like FileAlreadyExists(if such an exception exists) which then just opens it and writes?
Thanks for any help guys.
Is there a way of giving my app
permission so this doesn't happen?
You need the WRITE_EXTERNAL_STORAGE permission.
You also need to not have the SD card mounted on your development machine -- either the computer or the phone can access the SD card, but not both at the same time.
add WRITE_EXTERNAL_STORAGE permission to your manifest file
and if it is necessary do the following
plug the droid into your computer
pull down the manager (the bar on top with the time, sorry don't know the name, can't recall right now)
hit "usb connected"
window pops up on your computer showing you the contents of the SD card
hit "turn off usb storage" when you're done
disconnect usb cable (if you want) and then your Droid can read the SD card again