I am using Sql database in my appliaction,in that i want to take backup of the database.I have the following doubts:
1.I am running the application in emulator,for checking whether i have to plug some external storage to check ,if not in my system how can i check.
2.I am using the following code in my application,in that sdcard.write option is showing false,what wrong in this.
Follwing is my code:
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
java.lang.System.out.println("data="+sd.getAbsolutePath());
java.lang.System.out.println("data="+sd.canWrite());--->Showing as false
if (sd.canWrite()) {
String currentDBPath = "\\data\\com.budget\\databases\\budget";
String backupDBPath = "budget";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
java.lang.System.out.println("backup="+backupDB.getAbsolutePath());
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) {
}
Data Backup Documentation in android.
If your Android application has a database and you want your users to be able to backup the database and restore it as they see fit, you’ll need to mess about with database files.
Below is a class called DbExportImport. Once you’ve set it up with the correct values, all you need to do is call exportDb(), importDb() or restoreDb() from your application to perform the necessary operations.
This is also useful as a temporary measure when changing your package name or key for application signing, as your application will be newly installed and you will lose your database.(More).
However, you might want to extend BackupAgent directly if you need to: * Back up data in a database. If you have an SQLite database that you want to restore when the user re-installs your application, you need to build a custom BackupAgent that reads the appropriate data during a backup operation, then create your table and insert the data during a restore operation(More).
Now your can also backup from sdcard Backup and restore SQLite database to sdcard
Here is the another solution just like your problem.
If you are using Eclipse, go to Window - Android Virtual Device Manager, select the AVD you are using and click Edit, in the Hardware options select New and then select the SD Card Support. Then just define the storage size and click Edit AVD, to save the changes.
To check if sd is mounted use:
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
//code for sd mounted
}else{
//code for sd not mounted
}
Check if you have setted the permission in the manifest.
<manifest ...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
The file extension (.db) is missing in the excerpt above:
currentDBPath = "\\\data\\\com.budget\\\databases\\\budget.db";
Related
I know there have been questions about this, but for some reason nothing seems to work for me.
I'm trying to get 2 text files to save to the SD card from my app. It correctly creates the directory and the files, but always to the Internal Storage, never the External Storage. I do have the permissions in place as well in the Manifest.
try {
File sdCard = Environment.getExternalStorageDirectory();
File myFile = new File(sdCard.getAbsolutePath() + "/rlgl");
myFile.mkdir();
// myFile.createNewFile();
String newLine = System.getProperty("line.separator");
File file = new File(myFile, "rlgls.txt");
if(file.exists()) {
} else if (!file.exists()){
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
for (int i = 0; i < 30; i++) {
myOutWriter.append("0.0" + newLine);
}
myOutWriter.close();
fOut.close();
}
} catch (IOException e) {
e.printStackTrace();
}
This is the code that I am using. I've followed directions from other Stackoverflow responses but it never goes to the SD Card. Is there something I'm doing wrong? Also a follow up question is there a way for me to use the above code in order to make the files invisible to the user. They should have no reason to open them. Thanks in advance.
It correctly creates the directory and the files, but always to the Internal Storage, never the External Storage
No, it places them on external storage. What the user sees as internal storage is what the developer sees as external storage. Internal storage is accessed via methods like getFilesDir(). And none of those are removable storage, such as some form of SD card.
Also a follow up question is there a way for me to use the above code in order to make the files invisible to the user. They should have no reason to open them.
Then put them on internal storage.
my app can't read/write from/to the files when there is a "." in front of their names
I find that very difficult to believe. The . prefix makes them not show up by default in some file browsers, but that's it. Users can get to them (if they are on external storage), and apps can get to them (subject to the same rules as any other files, those without a leading .).
I am trying to copy data from DDBMS perspective -> file explorer -> data -> [my_app_package] then PULL this is working when I am running application on emulator but when I am running application on mobile nothing is shown under data folder.
I have also tried to do same thing from adb but its not working.. Is their any way to explore sqllite database created by my application without rooting my phone.
One can surely retreive database .db file from Android Device programmatically. I used to put one more setting under my application named Developer Options which copy .db file into sdCard.
Following code copy .db to sdcard. Change your copied .db file name into whatever like to with backupDBPath and currentDBPath (Name which you gave to your database name).
public void dev()
{
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "/data/data/" + getPackageName() + "/databases/ZnameDB";
String backupDBPath = "ZnameDB_Dev.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();
Toast.makeText(SettingsActivity.this, "Database Transfered!", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
There are many application available in PlayStore with which you can view your .db file. I used aSQLiteManager android application to view .db file.
No you can't,how ever you can use logs to fetch and see the fetched data in logcat or set some value on your textviews or use table layout too see your fetched data .However the best way is using SQLiteBrowser.You can pull the database file from your emulator or device and then save it on your hard disk and browse it in Sqlitebrowser.
Download SqliteBrowser from here http://sourceforge.net/projects/sqlitebrowser/
You can copy your sqlite file to sdcard programmatically. Follow this link. And view it using any Sqlite Viewer.
I have my Android application in which i m using SQLite database. Everything is working fine. But i want to download .db file to check my Database schema and data in database.
But i don't know how to copy SQLite .db file from android device.
i m using Google Nexus 7 for my development.
please help me on that
Thanks in advance
You can use the adb pull command to read a file from the teathered device to your desktop.
E.g. adb pull /data/data/com.foo.bar/databases/MyDatabase
Hi you want o Download SQLite Database Browser Click here to download and then in Eclipse select your project and go to DDMS window there you will find a Sub-window which consists of tabs click on File Explorer and Click on data folder a drop down folder will come and again click on the data folder Now you can see all your projects loaded in eclipse then select your project if it consists Database means it will show you database folder click on that you will find your DB select the DB you want click on Save Icon (Pull a file from device)
and then save your db. Now open the download SQLite db browser and browse the .db extension file you can view you Database .
use this method to copy database to external data storage of device
private void copyDbToExternal(Context context) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//data//" + context.getApplicationContext().getPackageName() + "//databases//"
+ DB_NAME;
String backupDBPath = DB_NAME;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
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();
}
}
I'm fairly new to apk development. So far, after a book purchase and with a lot of Googling I've managed to make an application that controls some features of my custom ROM. I'm currently trying to implement 2 backup features. I want to backup /data/system/batterystats.bin to /sdcard and also i want to backup launcher.db of my touchwiz launcher to /sdcard.
For the first part i haven't actually found anything. I've searched a lot about how to restore a file, not much has come up. It's mostly about SQL .db files. I've also looked for the possibility to run a shell script via the apk just to perform this backup. With a shell script it's easy work, but doing this via .java, i honestly have no clue.
Also, i've tried quite a lot of code to get my sqlite database file to backup, but i was quite unsuccessful. Here's my code for you to look at:
public class Backup extends Activity {
public void exportDB(){
try {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String currentDBPath = "data/data/com.sec.android.app.twlauncher/databases/launcher.db";
String backupDBPath = sd + "/launcher.db";
File currentDB = new File(currentDBPath);
File backupDB = new File(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();
}
}
}
I have added permissions for external storage write, of course in the androidmanifest, but nothing happens. No FC, it just sits there doing nothing. And when I check my sdcard, there's nothing there.
Any help would be greatly appreciated. Thanks
Download RootTools (the jar file).
You can then run linux commands like this:
RootTools.sendShell(command);
For example to backup, you could do:
RootTools.sendShell("cp -f /data/data/com.sec.android.app.twlauncher/databases/launcher.db /sdcard/directory/");
And to restore the file:
RootTools.sendShell("cp -f /sdcard/directory/launcher.db /data/data/com.sec.android.app.twlauncher/databases/");
cp is the copy command, and -f is what allows it to overwite the file if it already exists.
RootTools are great, and for the commands, just google how to do linux commands and then place them into the sendShell
I have no idea how to do it with java. I personally think that using the linux commands are 10 times easier, though.
And to note, it is actually good to get sdcard location like this:
Environment.getExternalStorageDirectory();
and then append the rest of the storage location info to the end of that.
I m using Android 1.5 my data directory doesn't have the read/write permissions
System.out.println("DAta can write??--->"+Environment.getDataDirectory().canWrite());
System.out.println("DAta can read??--->"+Environment.getDataDirectory().canRead());
So please suggest me how to provide permissions for the data directory.
What I'm trying to do is to create a file and add some content to it in the Data storage of the emulator like as below
private void writeToSDCard() {
try {
File lroot = Environment.getDataDirectory();
if (lroot.canWrite()){
File lfile = new File(lroot, "samplefile.txt");
FileWriter lfilewriter = new FileWriter(lfile);
BufferedWriter lout = new BufferedWriter(lfilewriter);
lout.write("XXXXXXXXXXXXXXXXXX");
lout.close();
}
} catch (IOException e) {
Log.e(m_cTAG, "Could not write file " + e.getMessage());
}
}
You shouldn't be looking at the Data Directory. This is a system directory in the phone's storage - usually /data - and your application will never have permission to write to it.
The directory your application should write files to is returned by the Context.getFilesDir() method. It will be something like /data/data/com.yourdomain.YourApp/files.
If you want to write to a file in the phone's storage use the Context.openFileOutput() method.
If you want the path to the SDCard then use Environment.getExternalStorageDirectory() method. To write to the SDCard you'll need to give your application the appropriate permissions by adding the following to your Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If you're going to write to the SDCard you'll also need to check its state with the getExternalStorageState() method.
If you're storing small files to do with your application then these can go into the phone's storage and not the SD Card, so use the Context.openFileOutput() and Context.openFileInput() methods.
So in your code consider something like:
OutputStream os = openFileOutput("samplefile.txt", MODE_PRIVATE);
BufferedWriter lout = new BufferedWriter(new OutputStreamWriter(os));
you need to locate your save build location, sometimes its automatically located in the read-only area on your pc