Pick a file from an SD card using Intent

user2978042 picture user2978042 · Apr 2, 2014 · Viewed 12k times · Source

Does Android have a way to browse and pick any file from an SD card using intents?

Something like:

String uri = (Environment.getExternalStorageDirectory()).getAbsolutePath();
Intent i = new Intent(Intent.ACTION_PICK, Uri.parse(uri));

I am trying to send a file to other devices using Bluetooth. I can send if I give the full path of the file name in my code. I want my users to pick the file that should be send.

Answer

Ketan Ahir picture Ketan Ahir · Apr 2, 2014

You can use the following code:

Intent mediaIntent = new Intent(Intent.ACTION_GET_CONTENT);
mediaIntent.setType("video/*"); // Set MIME type as per requirement
startActivityForResult(mediaIntent,REQUESTCODE_PICK_VIDEO);

Then you can get the path in onActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUESTCODE_PICK_VIDEO
            && resultCode == Activity.RESULT_OK) {
        Uri videoUri = data.getData();
        Log.d("", "Video URI= " + videoUri);
    }
}