I need to open folder in internal storage that contains images.
I use following code.
Java
File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyPhotos");
Intent intent = new Intent(Intent.ACTION_VIEW);
String path =folder.getPath();
Uri myImagesdir = Uri.parse("file://" + path );
intent.setDataAndType(myImagesdir,"*/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
PATHS
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="images" path="Pictures" />
<external-path name="external_files" path="."/>
<external-path name="files_root" path="Android/data/${applicationId}"/> </paths>
Manifest
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
xml/file_paths
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="images" path="Pictures" />
<external-path name="external_files" path="."/>
<external-path name="files_root" path="Android/data/${applicationId}"/>
</paths>
ERROR
FATAL EXCEPTION: main Process: android.apps.bnb.company.myphotos, PID: 22482 android.os.FileUriExposedException: file:///storage/emulated/0/Pictures/MyPhotos exposed beyond app through Intent.getData()
Is any another way to open folder in internal storage? Thanks!
UPDATE #1
Using this arcticle https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en I replaced
Uri myImagesdir = Uri.parse("file://" + path );
with
Uri myImagesdir = Uri.parse("content://" + path );
And the error gone.
Anyway I have to choose always app to open this folder.
Is it possibility to use My Files app by default to open certain folder?
The Below Code is used by me to open an image from Storage :
Edited Answer :
Yes & No. Its not 100% guaranteed that it will work on all devices.
Edit 1:
Following is one of the way with which it can be done. I have tested on few emulators (running Android N & Android O) and loads default file explorer:
MainActivity.java
AndroidManifest.xml
GenericFileProvider.java
file_paths.xml
The above approach doesn't work on big players like samsung
Alternatives
1. Using type
DocumentsContract.Document.MIME_TYPE_DIR
intent.setDataAndType(dirUri,DocumentsContract.Document.MIME_TYPE_DIR);
This approach works on several emulators and limited set of devices. It doesn't work with big players like Samsung or Huawei.
2. Using type
resource/folder
intent.setDataAndType(dirUri,"resource/folder");
This approach works only if user has installed ES file explorer app.
If you choose to use , then you have to check if any intent is available to handle it by using:
PackageManager packageManager = getActivity().getPackageManager();
3. Using type
*/*
intent.setDataAndType(dirUri,"*/*");
This approach works, if user chooses File Manager app from the Intent Chooser and mark it as default app to handle
*/*
. However it has some drawbacks (Thanks to @CommonsWare for bringing some of it out):4. Using type
text/csv
intent.setDataAndType(uri, "text/csv")
This will limit the number of apps which will displayed to the user but same limitations when
*/*
used is applied. Apps which can handlecsv
will be displayed and if user chooses it then the apps would crash.There are some device specific implementations available here as mentioned by @Academy of Programmer which requires to identify the default file manager's intent and extra's need by it.
Conclusion:
There is no standard type available to achieve it since there is no standard followed by the File Managers to support specific type at the moment. In future may be Google will come up with some approach. Best alternative would be to implement your own file manager just like Dropbox or Google Drive does. There are several libraries available which provide this feature.
I found solution here can we open download folder via. intent?
This code works perfect in my Samsung J7 to open Pictures folder (and others) from internal memory using Samsung default application My files.
It seems like we have to decompile File Manager of each manufacturer to see how to call it properly. :( And it is too much work. Well... I assumed there is some generic solution to do it.