package tlc.savetosd; // my package name ; activity package import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.view.Menu; import android.widget.EditText; import android.widget.Toast; public class Savetosd extends Activity { // my class name is: (short) Savetosd; (long) tlc.savetosd.Savetosd; you will have to enter the long name as the activityclass in app inventor @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_savetosd); final EditText input = new EditText(this); /**** * This Alert Dialog prompts the user for a file name to save ****/ new AlertDialog.Builder(this) .setTitle("Save As: ") .setMessage("Enter File Name: ") .setView(input) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String filename; Editable name = input.getText(); //gets the text from the input box filename = name+".txt"; // creates the filename with .txt extension savefile(filename); // calls the procedure to write the file with the filename as parameter } private void savefile(String filename) { String input = "nothing passed in"; Bundle extras = getIntent().getExtras(); // gets the extra data from the intent that started the activity(aka the AI app) input = extras.getString("test"); // this gets the data that has an "Extra Key" of name "test"(this can be anything as long as it is the same in both App Inventor and Eclipse try { File myFile = new File("/sdcard/TLC/"+ filename); // designates the path to save to (note that if the folder is not found it will pass an error and the app will crash) // todo //add exception handler for when the path does not exist myFile.createNewFile(); FileOutputStream fOut = new FileOutputStream(myFile); // creates the file OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); myOutWriter.append(input); // writes the received data to the file myOutWriter.close(); fOut.close(); Bundle bundle = new Bundle(); bundle.putCharSequence("APP_INVENTOR_RESULT",filename); Intent mIntent = new Intent(); // creates an intent to send app inventor a result mIntent.putExtras(bundle); setResult(RESULT_OK, mIntent); finish(); // closes the app and returns to app inventor } catch (Exception e) { Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); } finish(); } } ) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Bundle bundle = new Bundle(); bundle.putCharSequence("APP_INVENTOR_RESULT", "Opperation Cancelled, please try again"); Intent mIntent = new Intent(); mIntent.putExtras(bundle); setResult(RESULT_OK, mIntent); finish(); } }).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_savetosd, menu); return true; } }