michael
michael

Reputation: 110630

How can I look at the database of my application on a non-rooted phone

I have installed my android application (with debug on) on my non-rooted Nexus phone. My question is how can I look at the database created by my application?

Thank you.

Upvotes: 2

Views: 252

Answers (3)

Tolga E
Tolga E

Reputation: 12678

I've looked into this and unfortunately there's no practical way to do this. This is because you can't get access to the files on a non-rooted phone.

This is why whenever I need to access the database, i just fire up the emulator since you can easily get access to the database and even make changes on the fly and see how it effects the app.

Upvotes: 1

Rejinderi
Rejinderi

Reputation: 11844

you cannot do that but if its your own application you can actually go through the tedious way of exporting your database files to the sd card heres a utility method I came out with to help with that..

Change the string "/data/net.rejinderi.yourpackagehere/databases/yourdbnamehere.db" to fit your application, create an instance of the AsyncTask and call execute on it. that simple.

Be sure to include the permissions of using the external storage though..

Good luck. :)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.Toast;

public class ExportDatabaseFileTask extends AsyncTask<Void, Void, Boolean> {
    private final ProgressDialog progressDialog;

    private Context context;

    public ExportDatabaseFileTask(Context context)
    {
        this.context = context;
        progressDialog = new ProgressDialog(context);
    }

    protected void onPreExecute() {
       this.progressDialog.setMessage("Exporting database...");
       this.progressDialog.show();
    }

    protected Boolean doInBackground(Void... args) {
       File dbFile = new File(Environment.getDataDirectory() + "/data/net.rejinderi.yourpackagehere/databases/yourdbnamehere.db");

       File exportDir = new File(Environment.getExternalStorageDirectory(), "");
       if (!exportDir.exists()) {
          exportDir.mkdirs();
       }
       File file = new File(exportDir, dbFile.getName());

       try {
          file.createNewFile();
          this.copyFile(dbFile, file);
          return true;
       } catch (IOException e) {
          return false;
       }
    }

    protected void onPostExecute(final Boolean success) {
       if (this.progressDialog.isShowing()) {
          this.progressDialog.dismiss();
       }
       if (success) {
          Toast.makeText(context, "Export successful!", Toast.LENGTH_SHORT).show();
       } else {
          Toast.makeText(context, "Export failed", Toast.LENGTH_SHORT).show();
       }
    }

    void copyFile(File src, File dst) throws IOException {
       FileChannel inChannel = new FileInputStream(src).getChannel();
       FileChannel outChannel = new FileOutputStream(dst).getChannel();
       try {
          inChannel.transferTo(0, inChannel.size(), outChannel);
       } finally {
          if (inChannel != null)
             inChannel.close();
          if (outChannel != null)
             outChannel.close();
       }
    }
 }

Upvotes: 1

Asahi
Asahi

Reputation: 13506

for debug purposes you can programatically copy db file(s) from internal phone memory to external (SD card). they're just a files after all

Upvotes: 0

Related Questions