How to browse files for processing

As an android developer, you may at some point have to implement a way for users to choose files locally. Perhaps for file uploading or choosing a song to play.  The easiest way to handle this is using the storage access framework.

Android 5.0 extended this framework with  the OPEN_DOCUMENT_TREE intent. As of now, however, this only works for 5.0, so this tutorial will be focusing on using the OPEN_DOCUMENT intent.

Unfortunately, the OPEN_DOCUMENT method isn’t supported for versions 4.3 and below so you might consider checking the devices SDK version using Build.VERSION to decide whether to use this method or to use the PICK or GET_CONTENT methods, like so:

if(Build.VERSION.SDK_INT >= 19){
    //use this method
}
else{
    //use a different method
}

 


 

We’ll start with a minimal layout containing a single button to launch the browser.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.filebrowser.MainActivity" >

<Button
android:id="@+id/launch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Launch" />

</RelativeLayout>

Now we can write the function to launch the browser.

public void performFileSearch(){
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");    //Note: This can be changed to filter specific MIME types
    startActivityForResult(intent, 42);    //Note: 42 can be any number as long as it matches in onActivityResult
}

The parameter for the function on line 4 i the MIME type for files you are looking for. So if you want to make sure the user choose an image, change it to “image/*”. If you want the user to choose an audio file, change it to “audio/*”. And so on. The asterisk(*) simply means “all” in this context. Here is a list of different MIME types for reference.

Now we can call that method from the button:

//in onCreate method
Button startButton = (Button) findViewById(R.id.launch_button);
startButton.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        performFileSearch();
    }
});

Now we just have to decide what to do when a file is selected.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if (requestCode == 42 && resultCode == Activity.RESULT_OK) {
        // Note: the 42 above can be any number as long as it matches in
        //  the startActivityForResult call in performFileSearch()
        Uri uri = null;
        if (resultData != null) {
            uri = resultData.getData();
            //use the URI
        }
    }
}</pre>
<pre>

And now you can use the URI to play an audio file, view an image, upload the file to  a database, or whatever you plan to implement.

Sources:

http://developer.android.com/about/versions/android-5.0.html

http://developer.android.com/guide/topics/providers/document-provider.html

http://stackoverflow.com/questions/3093365/how-can-i-check-the-system-version-of-android

http://developer.android.com/reference/android/os/Build.VERSION.html