I want to get my Intent to open a file chooser in a specific folder of my app's external storage, not the default 'Recent' folder, in order to browse files. My targetSdkVersion is 28.
I have read: Trying open a specific folder in android using intent and Android: How to open a specific folder via Intent and show its content in a file browser? both have helped a little but not solved my specific problem.
The code below in the loadSavedProjectAction() method takes me to the phone's default Recent folder not my app's /Documents folder.
The variable StorageDirectory holds the correct path which is: /storage/emulated/0/Android/data/com.example.whereamihereattempt2/files/Documents
Here is my code:
AndroidManifest.xml includes:
<provider
android:name=".GenericFileProvider"
android:authorities="com.example.whereamihereattempt2.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent">
<external-path
name="WhereAmIHereAttempt2"
path="." />
</paths>
Java Fragment class and relevant methods only:
public class SavedProjectFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_saved_project, container, false);
Button selectSavedProjectButton = (Button) view.findViewById(R.id.select_saved_project_button_id);
if (Build.VERSION.SDK_INT >= 23) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 2);
}
selectSavedProjectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadSavedProjectAction();
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//not implemented yet
}
private void loadSavedProjectAction() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
File storageDirectory = getActivity().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
Uri uri = FileProvider.getUriForFile(getActivity(), "com.example.whereamihereattempt2.fileprovider", storageDirectory);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, "*/*");
Intent chooser = Intent.createChooser(intent, "Open folder");
startActivity(chooser);
}
}
If it is not possible to open to a specific folder I guess I will have to create my own file browser, are there any good libraries for this? Thanks for any help with this.