Monday, March 7, 2011

Android Intents How to pick a file with an Intent?

By Magesh Kumar   Posted at  4:33 AM   Android No comments


The code allows any application to call AndExplorer intent to pick a file on phone or SDCard. AndExplorer is free and available at: http://www.lysesoft.com/products/andexplorer/index.html 
int PICK_REQUEST_CODE = 0;
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
Uri startDir = Uri.fromFile(new File("/sdcard"));
// Files and directories !
intent.setDataAndType(startDir, "vnd.android.cursor.dir/lysesoft.andexplorer.file");
// Title
intent.putExtra("explorer_title", "Select a file");
// Optional colors
intent.putExtra("browser_title_background_color", "440000AA");
intent.putExtra("browser_title_foreground_color", "FFFFFFFF");
intent.putExtra("browser_list_background_color", "00000066");
// Optional font scale
intent.putExtra("browser_list_fontscale", "120%");
// Optional 0=simple list, 1 = list with filename and size, 2 = list with filename, size and date.
intent.putExtra("browser_list_layout", "2");
startActivityForResult(intent, PICK_REQUEST_CODE);

...

protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
   if (requestCode == PICK_REQUEST_CODE)
   {
   if (resultCode == RESULT_OK)
   {
      Uri uri = intent.getData();
      String type = intent.getType();
      LogHelper.i(TAG,"Pick completed: "+ uri + " "+type);
      if (uri != null)
      {
         String path = uri.toString();
         if (path.toLowerCase().startsWith("file://"))
         {
            // Selected file/directory path is below
            path = (new File(URI.create(path))).getAbsolutePath();
         }

      }
   }
   else LogHelper.i(TAG,"Back from pick with cancel status");
   }
}

About the Author

Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9

0 comments:

Back to top ↑
Connect with Us

What they says

© 2013 MaGeSH 2 help. WP Mythemeshop Converted by BloggerTheme9
Blogger templates. Proudly Powered by Blogger.