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!
1 Answers
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.

- 1
- 1

- 56
- 3
-
thanks a lot for the quick response. I was thinking of something similar to what you said but I wasn't sure what would be a good approach. thanks a lot. I would also appreciate other inputs!! Also, what would the file type be? – Georgi Angelov Jan 23 '13 at 22:34
-
The file would simply be saved as plain text (UTF-8 encoded if I am not mistaken) however the extension could be specified in whatever way you see fit - you could simply leave it without an extension (eg /sdcard/myfile) or use any extension you wish from .txt through to .abcxyz123 however this would not actually change the file type, just its name (for example an mp3 file renamed to have a .png extension is no less an audio file than it was previously, nor is it any more of an image other than by file name conventions) – rich200313 Jan 23 '13 at 22:45