Saturday, October 6, 2012

Saving output somewhere else

I wanted to allow users to save text data that is on their phone to third-party applications like to email or on Google Drive.  The best way to do this seems to be using ACTION_SEND intents as described here.

Code like this, will put the text you want to send into the body of an email message or will create a new document on Google Drive.
public void send_data() 
{
// Create an intent to send data out
Intent send_intent = new Intent(android.content.Intent.ACTION_SEND);
send_intent.setType("text/plain");


String data_to_send += "Sending this data string";
send_intent.putExtra(Intent.EXTRA_TEXT, data_to_send);

startActivity(send_intent);
}
Or, you can put the text data into an attachment with something like this.
public void send_data() 
{
String FILENAME = "sample.txt";
FileOutputStream fos;
try
{
// Set the file as world readable so other apps can read it.
fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);

// Update the file and close it.
String data_to_send += "Sending this data string";
fos.write(line_to_send.getBytes());
fos.close();
} catch (IOException e ){}

// Create an intent to send data out
String full_path_file = getFilesDir()+"/"+FILENAME;
Intent send_intent = new Intent(android.content.Intent.ACTION_SEND);
send_intent.setType("text/plain");
send_intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+full_path_file));
startActivity(send_intent);
}
This all works well, but when I try to open the document in google drive, I find that it isn't a native Google drive document, and the only thing I can do is view it.

I will update later again if I can find a way to improve this.

No comments:

Post a Comment