Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pick any kind of file via an Intent in Android

I would like to start an intentchooser for apps which can return any kind of file

Currently I use (which I copied from the Android email source code for file attachment)

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); Intent i = Intent.createChooser(intent, "File"); startActivityForResult(i, CHOOSE_FILE_REQUESTCODE); 

But it only shows "Gallery" and "Music player" on my Galaxy S2. There is a file explorer on this device and I would like it to appear in the list. I would also like the camera app to show in the list, so that the user can shoot a picture and send it through my app. If I install Astro file manager it will respond to that intent, too. My customers are Galaxy SII owners only and I don't want to force them to install Astro file manager given that they already have a basic but sufficient file manager.

Any idea of how I could achieve this ? I am pretty sure that I already saw the default file manager appear in such a menu to pick a file, but I can't remember in which app.

like image 830
ErGo_404 Avatar asked Jan 20 '12 17:01

ErGo_404


People also ask

What is intent file in Android?

An Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use cases: Starting an activity. An Activity represents a single screen in an app.

What is setType in intent Android?

setType(String mimeType) input param is represent the MIME type data that u want to get in return from firing intent(here myIntent instance). by using one of following MIME type you can force user to pick option which you desire. Please take a Note here, All MIME types in android are in lowercase.


2 Answers

Not for camera but for other files..

In my device I have ES File Explorer installed and This simply thing works in my case..

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("file/*"); startActivityForResult(intent, PICKFILE_REQUEST_CODE); 
like image 104
user370305 Avatar answered Oct 10 '22 15:10

user370305


Samsung file explorer needs not only custom action (com.sec.android.app.myfiles.PICK_DATA), but also category part (Intent.CATEGORY_DEFAULT) and mime-type should be passed as extra.

Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA"); intent.putExtra("CONTENT_TYPE", "*/*"); intent.addCategory(Intent.CATEGORY_DEFAULT); 

You can also use this action for opening multiple files: com.sec.android.app.myfiles.PICK_DATA_MULTIPLE Anyway here is my solution which works on Samsung and other devices:

public void openFile(String mimeType) {          Intent intent = new Intent(Intent.ACTION_GET_CONTENT);         intent.setType(mimeType);         intent.addCategory(Intent.CATEGORY_OPENABLE);          // special intent for Samsung file manager         Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");          // if you want any file type, you can skip next line          sIntent.putExtra("CONTENT_TYPE", mimeType);          sIntent.addCategory(Intent.CATEGORY_DEFAULT);          Intent chooserIntent;         if (getPackageManager().resolveActivity(sIntent, 0) != null){             // it is device with Samsung file manager             chooserIntent = Intent.createChooser(sIntent, "Open file");             chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { intent});         } else {             chooserIntent = Intent.createChooser(intent, "Open file");         }          try {             startActivityForResult(chooserIntent, CHOOSE_FILE_REQUESTCODE);         } catch (android.content.ActivityNotFoundException ex) {             Toast.makeText(getApplicationContext(), "No suitable File Manager was found.", Toast.LENGTH_SHORT).show();         }     } 

This solution works well for me, and maybe will be useful for someone else.

like image 28
Chupik Avatar answered Oct 10 '22 13:10

Chupik