César Castro Aroche César Castro Aroche - 2 months ago 26
Java Question

Get address from file explorer Android

I'm making a program that asks the user to find a file which could be stored on the sdcard or the internal card of the phone. I know that in order to open the default file explorer in the phone I must use this code:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivity(intent);


However, my problem is related with the communication with the File Explorer, how can I get the address of the file the user selected.

Answer
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("file/*");
    startActivityForResult(intent, YOUR_REQUEST_CODE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == RESULT_OK) {
    switch (requestCode) {
    case YOUR_REQUEST_CODE:
      //get the uri from data's extras
      break;
    }
     //do whatever you want with uri
  } else {
    Toast.makeText(this, "Wrong result", Toast.LENGTH_SHORT).show();
  }
}