how can we push ".db" into emulator? - android

Can we push a database that is created by some ide like sqlitestudio and push it into our emulator for app uses?
is there any way to push our ".db" format into andriod emulator?

I think you want to ship you application by creating database outside ,
these are good tutorial to add database to your application , and these are few good tutorials to start with
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
http://mfarhan133.wordpress.com/2010/10/24/database-crud-tutorial-for-android/
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/

If your device is an emulator or is a physical device connected thru USB, you can use this command-line:
adb push c:\local_path\myfile.db /path_on_the_device/myfile

You better add db into assets and copy it into sd or internal storage.
Here is some code snippet for you
private void CopyFileFromAssets() {
AssetManager asm = getAssets();
String[] files = null;
try {
files = asm.list("");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = asm.open(filename);
//you can even create folder to put your file
out = new FileOutputStream("/sdcard/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {
Log.e("tag", e.getMessage());
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
Hope this can help

Go to file explorer - data - data - your pkg name - select database and click on push a file onto the device.

First remove the db extension.
1.then select DDMS
2.then select emulator from device tab
3.move to data/data//databases
4.now push file into emulator using the top-right open in window.
5.Now run the app.
Thank you.

Related

Is There A Way To Store SharedPreferences to SDcard?

I've written an app that has several hard-coded settings such as fontSize or targetDirectory. I would like to be able to change those type of settings on an infrequent basis.
SharedPreferences seems to be one way to go about it but I want to share this application and settings, and my phone is not rooted.
My application is a personal tool, and does not have a UI. It opens, does it's job, and then closes. I could create the equivalent of a Windows .ini file and read/write to it, but that seems clunky. Having the SharePreferences file located on the sdcard where I can reach it, instead of device memory, where I can't, seems like that would work.
I do not want to backup these preferences, just be able to edit them, or copy them to a new device.
By default SharedPreferences files are stored in internal storage. You can make a backup of it to SD card programmatically.
File ff = new File("/data/data/"
+ MainActivity.this.getPackageName()
+ "/shared_prefs/pref file name.xml");
copyFile(ff.getPath().toString(), "your sdcard path/save file name.xml");
private void copyFile(String filepath, String storefilepath) {
try {
File f1 = new File(filepath);
File f2 = new File(storefilepath);
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
You may replace it back when first start and backup it when application closed.
References: here

Downloading Multiple files or folder from dropbox in android ?

So far I've successfully integrated Dropbox API with my project. For that I've been using an example program that is given in Dropbox SDK. From the way i can download(random Picture) and upload files easily. My question is, How can we download a folder or multiple files at a time from their dropbox account? . Additionally When i click on the download button it randomly chosen one image file then displaying, instead of doing this i want to download all image files or particular folder. Any suggestions or help would be appreciated.
Thanks in Advance.
Hi please go through following code, may be this is helpful to you.
private boolean downloadDropboxFile(String dbPath, File localFile) throws IOException{
BufferedInputStream br = null;
BufferedOutputStream bw = null;
try {
if (!localFile.exists()) {
localFile.createNewFile(); //otherwise dropbox client will fail silently
}
FileDownload fd = api.getFileStream("dropbox", dbPath, null);
br = new BufferedInputStream(fd.is);
bw = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[4096];
int read;
while (true) {
read = br.read(buffer);
if (read <= 0) {
break;
}
bw.write(buffer, 0, read);
}
} finally {
//in finally block:
if (bw != null) {
bw.close();
}
if (br != null) {
br.close();
}
}
return true;
}
For more information please check the source.
If you need to get all FILES and FOLDER from drop box, then simply copy and paste this code in your project and see the result. It will help you a lot.
String mPath="/"; //You can change the path here to specific FOLDER
Entry dirent = null;
try
{
dirent = mApi.metadata(mPath, 1000, null, true, null);
}
catch (DropboxException e)
{
System.out.println("Error Detail "+e.getMessage());
}
//Perform a Loop and retrieve all FILES and FOLDER from the PATH
for (Entry ent: dirent.contents)
{
String name = ent.fileName();
System.out.println("My File in Folder "+name);
}
The Sync API works by downloading files on-demand when you open them. So just open all the files you're trying to read.

Android work with db file from SD card

I wrote an app which logged data and saved it via SQL into a .db File. I had a method copying it from internal memory to SD card.
Now i wrote a second app, which needs to work with this particular .db file. As i think, that apps can't get access to package files from other apps
(in this case
/data/data/app1_package/databases/my_database.db
)
i need somehow to work with my DB on the SD Card. How do i do that?
Can i use this path in my SQLiteHelper class? Should i copy it from SD to my package, is that even possible (access rights etc.)?
I'm a beginner in databases, some help would be nice.
You can open any readable file path as a database:
File dbFile = new File( Environment.getExternalStorageDirectory(), "myfile.db" );
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile,null,null);
Note: check if sd-card is mounted before using this code.
yes place the DB file in your assets folder and get it this way :
DB_PATH="/data/data/app1_package/databases/my_database.db"
in your create :
is = getAssets().open("Meaniningss.db");
write(is);
the method :
public void write(InputStream is) {
try {
OutputStream out = new FileOutputStream(new File(DB_PATH));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = is.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
is.close();
out.flush();
out.close();
System.err.println(out + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}

How can I see the contents of a sqlite db of my app in a real device?

I want to see the contents of my database created in my app in the device I deploied it. I can use sqlite commands in shell and see in emulator but not in a real device.
Can anyone know how to do it in real device?
root is not necessary if the application is flagged a debuggable. You can use the run-as adb command.
e.g. to use sqlite :
adb shell "run-as com.your.package sqlite3 databases/database_name"
or copy and pull the database :
adb shell "run-as com.your.package cat databases/database_name > /sdcard/some_name"
adb pull /sdcard/some_name
unfortunately it seems that pre-ICS run-as has quite a few bugs, e.g. issue 20792 or 13965
Actually your Real Device is not Rooted so you don't have a permission to access /data/data/<Application>/database/ directory.
Only one solution copy that file to external Storage .. And see it in SQLiteManager or any other SQLite tools.
I think you need a rooted device to access sqlite.
Look at this discussion.
Try this,
You can copy your database in to your sdcard where you can see your database
public void backup() {
try {
File sdcard = Environment.getExternalStorageDirectory();
File outputFile = new File(sdcard,
"YourDatabase.db");
if (!outputFile.exists())
outputFile.createNewFile();
File data = Environment.getDataDirectory();
File inputFile = new File(data,
"data/"+getPackageName()+"/databases/"+"YourDatabase.db");
InputStream input = new FileInputStream(inputFile);
OutputStream output = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Copying Failed");
}
}
Hope it helps.
Try this function as
copyFile(new File("data/data/<app>/database"),new File("mnt/sdcard/....")).
public void copyFile(File filesrc, File filedst)
{
try
{
FileInputStream localFileInputStream = new FileInputStream(filesrc);
FileOutputStream localFileOutputStream = new FileOutputStream(filedst);
byte[] arrayOfByte = new byte[1024];
while (true)
{
int i = localFileInputStream.read(arrayOfByte);
if (i == -1)
{
localFileOutputStream.close();
localFileInputStream.close();
break;
}
localFileOutputStream.write(arrayOfByte, 0, i);
}
}
catch (Exception localException)
{
Log.d("XXX", "ExceptioncopyFile:" + localException.getMessage(), localException);
}
}

Android:Virtual machine cannot download file from website

I have written an app to download a .PDF file from the given url path.
But when I run it in the vm,I found my app cannot download the file.
So I tryed the DownloadManager in the vm,still it cannot download the file .
And the DownloadManage stopped with just and always showing "OnGoing".
Can anyone tell me the reason and how to solve this problem?
Thanks in advance!
PS:I write this
public File writeFileToSDCard(String path,String fileName,InputStream in){
File file = null;
OutputStream out = null;
try{
if(!createDir(path)){
Log.e("FileUtils.java", "writeFileToSDCard().createDir() failed!");
}else{
Log.e("FileUtils.java", "writeFileToSDCard().createDir() Successed!");
}
file = createFile(path+fileName);
out = new FileOutputStream(file);
byte buffer[] = new byte[128];
//write the file
do{
int length = in.read(buffer);
if(length != -1){
Log.e("FileUtils.java", "Writing to card!");
out.write(buffer, 0, length);
}else{
break;
}
}while(true);
}catch(Exception e){
Log.e("FileUtils.java", "writeFileToSDCard() failed!");
}finally {
try{
out.close();
}catch (Exception e) {
Log.e("FileUtils.java", "writeFileToSDCard() finally failed!");
}
}
return file;
}
*PSS:*I dont think my code goes wrong,but Is it the problem of the android emulator?
Most probably it is because you don't have permission to download the file from the server.
I had a similar problem few days back, I solved it by creating a .htacces file in the server directory. Create a file named .htacess containing this :
satisfy any
and save it in the directory where your pdf file is. I am not familiar with how this works, but I guess it has something to do with the permissions. Note that if you do this anyone can access the contents of that directory. And I believe this will only wirk for Apache servers.
Don't know if it will work, but its worth a shot.

Categories

Resources