Search code examples
androidandroid-intentbluetoothandroid-bluetoothandroid-implicit-intent

Pick a file from an SD card using Intent


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.


Solution

  • 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);
        }
    }