2 * Copyright (C) 2008 OpenIntents.org
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openintents.filemanager.demo;
21 import org.openintents.intents.FileManagerIntents;
23 import android.app.Activity;
24 import android.content.ActivityNotFoundException;
25 import android.content.Intent;
26 import android.database.Cursor;
27 import android.net.Uri;
28 import android.os.Bundle;
29 import android.provider.MediaStore;
30 import android.provider.MediaStore.Images;
31 import android.view.View;
32 import android.widget.EditText;
33 import android.widget.TextView;
34 import android.widget.Toast;
36 public class Demo extends Activity {
38 protected static final int REQUEST_CODE_PICK_FILE_OR_DIRECTORY = 1;
39 protected static final int REQUEST_CODE_GET_CONTENT = 2;
41 protected EditText mEditText;
42 protected TextView mTextView;
44 /** Called when the activity is first created. */
46 public void onCreate(Bundle savedInstanceState) {
47 super.onCreate(savedInstanceState);
48 setContentView(R.layout.main);
50 mEditText = (EditText) findViewById(R.id.file_path);
51 mTextView = (TextView) findViewById(R.id.info);
54 public void onClickOpenFile(View view) {
58 public void onClickSaveFile(View view) {
62 public void onClickPickDirectory(View view) {
66 public void onClickGetContent(View view) {
70 public void onClickOpenUri(View view){
75 * Opens the file manager to select a file to open.
77 public void openFile() {
78 String fileName = mEditText.getText().toString();
80 Intent intent = new Intent(FileManagerIntents.ACTION_PICK_FILE);
82 // Construct URI from file name.
83 File file = new File(fileName);
84 intent.setData(Uri.fromFile(file));
86 // Set fancy title and button (optional)
87 intent.putExtra(FileManagerIntents.EXTRA_TITLE, getString(R.string.open_title));
88 intent.putExtra(FileManagerIntents.EXTRA_BUTTON_TEXT, getString(R.string.open_button));
91 startActivityForResult(intent, REQUEST_CODE_PICK_FILE_OR_DIRECTORY);
92 } catch (ActivityNotFoundException e) {
93 // No compatible file manager was found.
94 Toast.makeText(this, R.string.no_filemanager_installed,
95 Toast.LENGTH_SHORT).show();
100 * Opens the file manager to select a location for saving a file.
102 private void saveFile() {
103 String fileName = mEditText.getText().toString();
105 Intent intent = new Intent(FileManagerIntents.ACTION_PICK_FILE);
107 // Construct URI from file name.
108 File file = new File(fileName);
109 intent.setData(Uri.fromFile(file));
111 // Set fancy title and button (optional)
112 intent.putExtra(FileManagerIntents.EXTRA_TITLE, getString(R.string.save_title));
113 intent.putExtra(FileManagerIntents.EXTRA_BUTTON_TEXT, getString(R.string.save_button));
116 startActivityForResult(intent, REQUEST_CODE_PICK_FILE_OR_DIRECTORY);
117 } catch (ActivityNotFoundException e) {
118 // No compatible file manager was found.
119 Toast.makeText(this, R.string.no_filemanager_installed,
120 Toast.LENGTH_SHORT).show();
125 * Opens the file manager to pick a directory.
127 private void pickDirectory() {
128 String fileName = mEditText.getText().toString();
130 // Note the different intent: PICK_DIRECTORY
131 Intent intent = new Intent(FileManagerIntents.ACTION_PICK_DIRECTORY);
133 // Construct URI from file name.
134 File file = new File(fileName);
135 intent.setData(Uri.fromFile(file));
137 // Set fancy title and button (optional)
138 intent.putExtra(FileManagerIntents.EXTRA_TITLE, getString(R.string.pick_directory_title));
139 intent.putExtra(FileManagerIntents.EXTRA_BUTTON_TEXT, getString(R.string.pick_directory_button));
142 startActivityForResult(intent, REQUEST_CODE_PICK_FILE_OR_DIRECTORY);
143 } catch (ActivityNotFoundException e) {
144 // No compatible file manager was found.
145 Toast.makeText(this, R.string.no_filemanager_installed,
146 Toast.LENGTH_SHORT).show();
151 * Use GET_CONTENT to open a file.
153 public void getContent() {
155 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
156 intent.setType("*/*");
157 intent.addCategory(Intent.CATEGORY_OPENABLE);
160 startActivityForResult(intent, REQUEST_CODE_GET_CONTENT);
161 } catch (ActivityNotFoundException e) {
162 // No compatible file manager was found.
163 Toast.makeText(this, R.string.no_filemanager_installed,
164 Toast.LENGTH_SHORT).show();
169 * Use URI to get a file
171 public void openFileUri(){
172 String filePath = mEditText.getText().toString();
173 Uri uri = Uri.parse("file://" + filePath);
175 Intent intent = new Intent(Intent.ACTION_VIEW);
179 //intent.setPackage("org.openintents.filemanager");
182 startActivity(intent);
183 } catch (ActivityNotFoundException e) {
184 // No compatible file manager was found.
185 Toast.makeText(this, R.string.no_filemanager_installed,
186 Toast.LENGTH_SHORT).show();
191 * This is called after the file manager finished.
194 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
195 super.onActivityResult(requestCode, resultCode, data);
197 mTextView.setText("");
199 switch (requestCode) {
200 case REQUEST_CODE_PICK_FILE_OR_DIRECTORY:
201 if (resultCode == RESULT_OK && data != null) {
202 // obtain the filename
203 Uri fileUri = data.getData();
204 if (fileUri != null) {
205 String filePath = fileUri.getPath();
206 if (filePath != null) {
207 mEditText.setText(filePath);
212 case REQUEST_CODE_GET_CONTENT:
213 if (resultCode == RESULT_OK && data != null) {
214 String filePath = null;
216 String displayName = null;
217 Uri uri = data.getData();
218 Cursor c = getContentResolver().query(uri, new String[] {MediaStore.MediaColumns.DATA,
219 MediaStore.MediaColumns.MIME_TYPE,
220 MediaStore.MediaColumns.DISPLAY_NAME,
221 MediaStore.MediaColumns.SIZE
222 }, null, null, null);
223 if (c != null && c.moveToFirst()) {
224 int id = c.getColumnIndex(Images.Media.DATA);
226 filePath = c.getString(id);
228 displayName = c.getString(2);
229 fileSize = c.getLong(3);
231 if (filePath != null) {
232 mEditText.setText(filePath);
233 String strFileSize = getString(R.string.get_content_info,
234 displayName, "" + fileSize);
235 mTextView.setText(strFileSize);