Search code examples
androidandroid-intenturiandroid-file

How to let user select only local files using Intent in Android?


I need to let the user select a file from their local storage for my application. Right now I'm using INTENT.ACTION_GET_CONTENT to let the user select the file, but it also gives the option of selecting URI from the cloud. After I get the URI file, I treat it as a local file and perform various things (including extracting the file extension). How can I let the user only pick local files?

else if(menuItem.getItemId() == R.id.action_import_from_file){
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.setType("*/*");
    startActivityForResult(Intent.createChooser(i, "Pick a file"), REQUEST_CODE_SELECT_FILE_FOR_IMPORT);
}

Solution

  • Please try using Intent.EXTRA_LOCAL_ONLY like
    intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    If the gallery app in your device supports it, only files in local storage will be displayed. If the gallery app does not support the above intent, you can show an error message to the user after checking the file length of the returned file path from gallery app.