1 package org.openintents.filemanager;
\r
3 import java.io.BufferedInputStream;
\r
4 import java.io.BufferedOutputStream;
\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
13 import org.openintents.intents.FileManagerIntents;
\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
25 public class SaveAsActivity extends Activity {
\r
26 protected static final int REQUEST_CODE_PICK_FILE_OR_DIRECTORY = 1;
\r
28 //Whether the scheme is file: (otherwise it's content:)
\r
29 private boolean fileScheme = false;
\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
41 if(uri.getScheme().equals("file"))
\r
43 else if(uri.getScheme().equals("content"))
\r
44 processContent(uri);
\r
47 Toast.makeText(this, R.string.saveas_no_file_picked, Toast.LENGTH_SHORT).show();
\r
51 private void startPickActivity(Intent intent){
\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
60 private Intent createPickIntent(){
\r
61 return new Intent(FileManagerIntents.ACTION_PICK_FILE);
\r
64 private void processFile(Uri uri){
\r
66 Intent intent = createPickIntent();
\r
67 intent.setData(uri);
\r
68 startPickActivity(intent);
\r
71 private void processContent(Uri uri){
\r
73 String name = getPath(uri);
\r
74 Intent intent = createPickIntent();
\r
75 intent.setData(Uri.parse(name));
\r
76 startPickActivity(intent);
\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
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
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
96 return sd.getPath();
\r
100 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
\r
101 super.onActivityResult(requestCode, resultCode, data);
\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
114 finish(); //End the activity
\r
117 private void saveFile(File destination){
\r
118 InputStream in = null;
\r
119 OutputStream out = null;
\r
122 in = new BufferedInputStream(new FileInputStream(source.getPath()));
\r
124 in = new BufferedInputStream(getContentResolver().openInputStream(source));
\r
126 out = new BufferedOutputStream(new FileOutputStream(destination));
\r
127 byte[] buffer = new byte[1024];
\r
129 while(in.read(buffer) != -1)
\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
142 } catch (IOException e) {}
\r
147 } catch (IOException e) {}
\r