Georgi Angelov
Georgi Angelov

Reputation: 4388

What method to use to Export specific data from my Database and load it into the database on a different phone with the same app

So I want my app to have the ability for the user to be able to export values from the Database(in this case entries that the user created) and give them to another phone with the same app so they can import those values and directly insert them in their databse so both phones will have the same data. The data is not the whole database but only a portion of it depending on what the user want's to share. I don't want to use webservice as I want to have a tangible file to work it. So say I export my database values into a file and send them through, say e-mail, but I want the user to just be able to select the folder where to export the file and to personally go to the standart text messaging app and attach the file to the text message, as they would a picture. What are my options in this case? Thanks!

Upvotes: 0

Views: 212

Answers (1)

rich200313
rich200313

Reputation: 56

I think the best way would be to generate some form of parsable file to represent the data you wish to share which you could then save to a file (I would use JSON personally however XML etc could also be used).

To generate the required JSON you could follow a similar method to below (all classes below besides the obvious "MyDataClass" are included in the Android SDK or in Java)

    JSONArray export = new JSONArray();
    for(MyDataClass model : exportObjects)
    {
        JSONObject obj = new JSONObject();
        obj.put("attribute1", model.getAttribute1());
        obj.put("attribute2", model.getAttribute2());
        ...

        export.put(obj);
    }

    String jsonString = export.toString();

This will create a valid JSON string which you will be able to write to a file using the standard Java File classes and appropriate Uses Permission in the manifest (android.permission.WRITE_ETERNAL_STORAGE)

    File outFile = new File("Absolute save path");
    outFile.getParentFile().mkdirs();               //Ensure the parent directory exists
    FileOutputStream outputStream = 
            new FileOutputStream(outFile);          //Create a stream to access the file
    outputStream.write(jsonString.getBytes());      //Write the data
    outputStream.flush();
    outputStream.close();                           //Cleanup

How you determine the value of "Absolute save path" would depend largely on your personal preference and could be obtained in a number of ways eg

  • Specify/generate a path in code
  • Prompt user for a textual save location or file name to be combined with an absolute directory provided in code (note that UX is not my strong point :P)

I would also assume that you could use an Intent to launch a file browser activity to specify a save location or an external library such as here

The receiving app would then load this file to a string using something along the lines of

    String loadedJSON = "";
    File inFile = new File("Absolute path");
    FileInputStream fis = new FileInputStream(inFile);
    byte[] buffer = new byte[1024];
    int readBytes;
    while((readBytes = fis.read(buffer, 0, 1024))>=0)
    {
        loadedJSON.concat(new String(buffer, 0, readBytes));
    }
    fis.close();

Then simply reverse the process of creating the JSONArray in the first step

    List<MyDataClass> objectModels = new ArrayList<MyDataClass>();
    JSONArray parsedData = new JSONArray(loadedJSON);
    for(int i = 0; i<parsedData.length(); ++i)
    {
        JSONObject jObj = parsedData.getJSONObject(i);
        MyDataClass newObjectModel = new MyDataClass();
        newObjectModel.setAttribute1(jObj.getString("attribute1"));
        newObjectModel.setAttribute2(jObj.getInt("attribute2"));
        ...

        objectModels.add(newObjectModel);
    }

Then insert each of these parsed object models into your database however best suits your application.

Upvotes: 1

Related Questions