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

Viewed 2227

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);
}
3 Answers

Try this for only open csv and xlsx file it will select only csv and xlsx file

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    String[] mimetypes = {"application/vnd.openxmlformats- 
    officedocument.spreadsheetml.sheet", "text/csv"}; 
    intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
    startActivityForResult(intent, READ_REQUEST_CODE);
Related