1

I would like to export my database to my sdcard. I am trying to follow this question: Making a database backup to SDCard on Android

But since I'm still pretty new to programming, I'm not sure If I'm doing this right. I made a separate class for ExportDatabaseFileTask.java so now I just have to call it in my main.

In my main I have an onClick listener that executes this code:

        ExportDatabaseFileTask thing = new ExportDatabaseFileTask();

Is that line enough to run the code in my class to export the database?

Community
  • 1
  • 1
EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • Depends on the code you did put in ExportDatabaseFileTask.... – Frank Nov 13 '12 at 12:48
  • I believe I put in the correct code. I just changed "private" to "public" and added in `Context ctx;` towards the top. Now I don't have any redlines from eclipse. – EGHDK Nov 13 '12 at 12:49

2 Answers2

2

You will have to chedule the task:

ExportDatabaseFileTask thing = new ExportDatabaseFileTask();
thing.execute(.....);

Now it will be run from a new tread.

You are trying to access the external storage. Make sure you have necessary permission defined in your Manifest file. This can be done by adding

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

You can read the documentation here.

Frank
  • 16,476
  • 7
  • 38
  • 51
  • Wait, so all I have to put is .execute? Or do I need to put something where you put the "...."? – EGHDK Nov 13 '12 at 12:55
  • Wait... from the documentation it says "AsyncTask must be subclassed to be used." What does that mean? – EGHDK Nov 13 '12 at 12:56
  • If you used the code in the provided link, you can pass a String (but i don't see if it is used...) inside the execute. yes your class does extend AsynncTask – Frank Nov 13 '12 at 12:58
  • What string do I have to pass? It doesn't look like I need to pass anything. – EGHDK Nov 13 '12 at 12:59
  • My error seems to be "11-13 08:05:33.280: E/dalvikvm(4902): Unable to open stack trace file '/data/anr/traces.txt': Is a directory" My code is the code from the link in my question. and the code in your answer is in my main activity onClick. – EGHDK Nov 13 '12 at 13:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19480/discussion-between-eghdk-and-frank) – EGHDK Nov 13 '12 at 13:09
-1

Depending on the code you put in ExportDatabaseFileTask, you can just create an object of that class.

Each time you create an object with .... = new .....(); you are running the constructor; the constructor runs the code that is inside the

public ExportDatabaseFileTask()
{
}

in this case. Put your code in there, and it will run as soon as you create an ExportDatabaseFileTask object.

Or, in the class ExportDatabaseFileTask, create a method with the code

public void exportToSD()
{
//dostuff
}

then call thing.exportToSD();

user1810737
  • 234
  • 4
  • 14