I'm trying to open a video file stored in my device using the default media player(VLC). Code:
public void startPlay(View view) {
String name = "VideoName.mkv";
Uri path = FileProvider.getUriForFile(this, "com.packagName.AppName",
new File("storage/[sd card directory]/Videos/"+name));
Log.d("PATHVAR", path.toString());
Intent shareIntent = new Intent(Intent.ACTION_VIEW);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("video/*");
startActivity(shareIntent);
}
On the other hand, when I use Environment.getExternalStorageDirectory().getAbsolutePath()
, it doesn't throw any errors.
Manifest:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.packageName.AppName"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
Provider paths:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
<external-path name="external" path="." />
<external-files-path name="external_files" path="." />
<cache-path name="cache" path="." />
<external-cache-path name="external_cache" path="." />
<files-path name="files" path="." />
</paths>
Logcat:
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/6262-6133/Videos/VideoName.mkv
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
at com.packageName.AppName.MainActivity.startPlay(MainActivity.java:201)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:7339)
at android.widget.TextView.performClick(TextView.java:14222)
at android.view.View.performClickInternal(View.java:7305)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27787)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7091)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Any idea why this is happening? I've set up the necessary permissions and am able to create a text file in internal storage. I've searched around for a solution to this but most of them are using internal storage, not the sd card. Thanks.