英文:
Downloaded file share to other app like Whasapp, Facebook from my app
问题
java.lang.IllegalArgumentException: 无法找到包含 /storage/emulated/0/VideoDownloadFAST/20200816190612.mp4 的已配置根目录
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
at com.example.appname.HomeActivity$1$1.onMenuItemClick(HomeActivity.java:101)
路径文件代码:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external_files name="external_files" path="." />
</paths>
AndroidManifest 文件代码:
<provider android:name="androidx.core.content.FileProvider" android:authorities="com.example.appname.provider"
android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
</provider>
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("video/mp4");
share.putExtra(Intent.EXTRA_SUBJECT, "abc");
share.putExtra(Intent.EXTRA_TITLE, "abcd");
File imageFileToShare = new File(Environment.getExternalStorageDirectory() + "/VideoDownloadFAST/" + name);
Uri uri = FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
BuildConfig.APPLICATION_ID + ".provider", imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("com.example.appname");
startActivity(Intent.createChooser(share, "Message"));
英文:
THIS error show my code i try set share option in my app for downloaded video
> java.lang.IllegalArgumentException: Failed to find configured root
> that contains /storage/emulated/0/VideoDownloadFAST/20200816190612.mp4
> at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
> at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
> at com.example.appname.HomeActivity$1$1.onMenuItemClick(HomeActivity.java:101)
>
path file code
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external_files name="external_files" path="." />
</paths>
> AndroidManifest file code
<provider android:name="androidx.core.content.FileProvider" android:authorities="com.example.appname.provider"
android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
</provider>
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("video/mp4");
share.putExtra(Intent.EXTRA_SUBJECT, "abc");
share.putExtra(Intent.EXTRA_TITLE, "abcd");
File imageFileToShare = new File(Environment.getExternalStorageDirectory() + "/VideoDownloadFAST/" + name);
Uri uri = FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
BuildConfig.APPLICATION_ID + ".provider", imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("com.example.appname");
startActivity(Intent.createChooser(share, "Message"));
答案1
得分: 0
改变你的 provider.xml 文件,使其更加广泛,更加方便:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>
然后在 Java 中:
public static void shareVideo(Context context, String fullPath) {
try {
File file = new File(fullPath);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri videoUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, videoUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setData(videoUri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(intent, "分享标题"));
} else {
Uri videoUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, videoUri);
intent.setDataAndType(videoUri, "video/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(intent, "分享标题"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
不要忘记将以下内容添加到你的 AndroidManifest.xml
文件中:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
英文:
Change your provider.xml to a broader extend which is more convenient:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>
And then in java:
public static void shareVideo(Context context, String fullPath) {
try {
File file = new File(fullPath);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri videoUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, videoUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setData(videoUri);
intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(intent, "share title"));
} else {
Uri videoUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, videoUri);
intent.setDataAndType(videoUri, "video/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(intent, "share title"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
Don't forget to add this to your AndroidManifest.xml
file:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论