简体   繁体   中英

How to start Intent.ACTION_GET_CONTENT in a specific location?

I am trying to open a directory for user to choose an excel document from it. However, my file picker opens in the "recent" location. Is there a way to start it from a specific directory using new versions of Android?

This is my code:

 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                    Uri uri = Uri.parse(Environment.DIRECTORY_DOCUMENTS);
                    intent.setDataAndType(uri, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                    startActivity(Intent.createChooser(intent, "Open folder"));

You need to set EXTRA_INITIAL_URI:

intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)

Unfortunately, the Intent.ACTION_GET_CONTENT does not support opening in a specific location. The only way to choose a specific location is by using a third-party file picker library, such as FilePicker, aFileChooser, etc.

You can use these libraries to create a custom file picker that opens in a specific location and allows the user to choose a file. Then, you can call the custom file picker in your code instead of using Intent.ACTION_GET_CONTENT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM