| 1 | package org.openintents.filemanager;\r |
| 2 | \r |
| 3 | import java.io.BufferedInputStream;\r |
| 4 | import java.io.BufferedOutputStream;\r |
| 5 | import java.io.File;\r |
| 6 | import java.io.FileInputStream;\r |
| 7 | import java.io.FileNotFoundException;\r |
| 8 | import java.io.FileOutputStream;\r |
| 9 | import java.io.IOException;\r |
| 10 | import java.io.InputStream;\r |
| 11 | import java.io.OutputStream;\r |
| 12 | \r |
| 13 | import org.openintents.intents.FileManagerIntents;\r |
| 14 | \r |
| 15 | import android.app.Activity;\r |
| 16 | import android.content.ActivityNotFoundException;\r |
| 17 | import android.content.Intent;\r |
| 18 | import android.database.Cursor;\r |
| 19 | import android.net.Uri;\r |
| 20 | import android.os.Bundle;\r |
| 21 | import android.os.Environment;\r |
| 22 | import android.widget.RelativeLayout;\r |
| 23 | import android.widget.Toast;\r |
| 24 | \r |
| 25 | public class SaveAsActivity extends Activity {\r |
| 26 | protected static final int REQUEST_CODE_PICK_FILE_OR_DIRECTORY = 1;\r |
| 27 | private Uri source;\r |
| 28 | //Whether the scheme is file: (otherwise it's content:)\r |
| 29 | private boolean fileScheme = false;\r |
| 30 | \r |
| 31 | \r |
| 32 | @Override\r |
| 33 | public void onCreate(Bundle savedInstanceState) {\r |
| 34 | super.onCreate(savedInstanceState);\r |
| 35 | //This activity is never shown to the user.\r |
| 36 | setContentView(new RelativeLayout(this));\r |
| 37 | Intent receivedIntent = getIntent();\r |
| 38 | if(receivedIntent != null){\r |
| 39 | Uri uri = receivedIntent.getData();\r |
| 40 | source = uri;\r |
| 41 | if(uri.getScheme().equals("file"))\r |
| 42 | processFile(uri);\r |
| 43 | else if(uri.getScheme().equals("content"))\r |
| 44 | processContent(uri);\r |
| 45 | }\r |
| 46 | else{\r |
| 47 | Toast.makeText(this, R.string.saveas_no_file_picked, Toast.LENGTH_SHORT).show();\r |
| 48 | }\r |
| 49 | }\r |
| 50 | \r |
| 51 | private void startPickActivity(Intent intent){\r |
| 52 | try {\r |
| 53 | startActivityForResult(intent, REQUEST_CODE_PICK_FILE_OR_DIRECTORY);\r |
| 54 | } catch (ActivityNotFoundException e) {\r |
| 55 | //Should never happen, but Java requires this catch\r |
| 56 | Toast.makeText(this, R.string.saveas_error, Toast.LENGTH_SHORT).show();\r |
| 57 | }\r |
| 58 | }\r |
| 59 | \r |
| 60 | private Intent createPickIntent(){\r |
| 61 | return new Intent(FileManagerIntents.ACTION_PICK_FILE);\r |
| 62 | }\r |
| 63 | \r |
| 64 | private void processFile(Uri uri){\r |
| 65 | fileScheme = true;\r |
| 66 | Intent intent = createPickIntent();\r |
| 67 | intent.setData(uri);\r |
| 68 | startPickActivity(intent);\r |
| 69 | }\r |
| 70 | \r |
| 71 | private void processContent(Uri uri){\r |
| 72 | fileScheme = false;\r |
| 73 | String name = getPath(uri);\r |
| 74 | Intent intent = createPickIntent();\r |
| 75 | intent.setData(Uri.parse(name));\r |
| 76 | startPickActivity(intent);\r |
| 77 | }\r |
| 78 | \r |
| 79 | /*\r |
| 80 | * Get the default path and filename for the saved file from content: scheme.\r |
| 81 | * As the directory is always used the SD storage.\r |
| 82 | * For GMail, the filename is the _display_name in its ContentProvider. Otherwise the file has\r |
| 83 | * no name.\r |
| 84 | * !IMPORTANT! When you add another "special" intent-filter like the one for GMail, consider,\r |
| 85 | * if you could add also the code for finding out the filename.\r |
| 86 | */\r |
| 87 | private String getPath(Uri uri){\r |
| 88 | Uri sd = Uri.fromFile(Environment.getExternalStorageDirectory());\r |
| 89 | if(uri.getHost().equals("gmail-ls")){\r |
| 90 | Cursor cur = managedQuery(uri, new String[]{"_display_name"}, null, null, null);\r |
| 91 | int nameColumn = cur.getColumnIndex("_display_name"); \r |
| 92 | if(cur.moveToFirst()){\r |
| 93 | return sd.buildUpon().appendPath(cur.getString(nameColumn)).toString();\r |
| 94 | }\r |
| 95 | }\r |
| 96 | return sd.getPath();\r |
| 97 | }\r |
| 98 | \r |
| 99 | @Override\r |
| 100 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {\r |
| 101 | super.onActivityResult(requestCode, resultCode, data);\r |
| 102 | \r |
| 103 | switch (requestCode) {\r |
| 104 | case REQUEST_CODE_PICK_FILE_OR_DIRECTORY:\r |
| 105 | if (resultCode == RESULT_OK && data != null) {\r |
| 106 | Uri destinationUri = data.getData();\r |
| 107 | if (destinationUri != null && source != null) {\r |
| 108 | String destinationPath = destinationUri.getPath();\r |
| 109 | saveFile(new File(destinationPath));\r |
| 110 | }\r |
| 111 | }\r |
| 112 | break;\r |
| 113 | }\r |
| 114 | finish(); //End the activity\r |
| 115 | }\r |
| 116 | \r |
| 117 | private void saveFile(File destination){\r |
| 118 | InputStream in = null;\r |
| 119 | OutputStream out = null;\r |
| 120 | try {\r |
| 121 | if(fileScheme)\r |
| 122 | in = new BufferedInputStream(new FileInputStream(source.getPath()));\r |
| 123 | else\r |
| 124 | in = new BufferedInputStream(getContentResolver().openInputStream(source));\r |
| 125 | \r |
| 126 | out = new BufferedOutputStream(new FileOutputStream(destination));\r |
| 127 | byte[] buffer = new byte[1024];\r |
| 128 | \r |
| 129 | while(in.read(buffer) != -1)\r |
| 130 | out.write(buffer);\r |
| 131 | Toast.makeText(this, R.string.saveas_file_saved, Toast.LENGTH_SHORT).show();\r |
| 132 | } catch(FileNotFoundException e){\r |
| 133 | //Should never get here\r |
| 134 | Toast.makeText(this, R.string.saveas_error, Toast.LENGTH_SHORT).show();\r |
| 135 | } catch(IOException e){\r |
| 136 | Toast.makeText(this, R.string.saveas_error, Toast.LENGTH_SHORT).show();\r |
| 137 | }\r |
| 138 | finally{\r |
| 139 | if (in != null) {\r |
| 140 | try {\r |
| 141 | in.close();\r |
| 142 | } catch (IOException e) {}\r |
| 143 | }\r |
| 144 | if (out != null) {\r |
| 145 | try {\r |
| 146 | out.close();\r |
| 147 | } catch (IOException e) {}\r |
| 148 | }\r |
| 149 | \r |
| 150 | }\r |
| 151 | }\r |
| 152 | }\r |