java - 如何在另一个应用程序中共享来自内部存储的媒体?
问题描述
我已经尝试解决这个问题将近 3 天了,但我根本无法成功。我查看了所有可能的主题,但没有任何效果。
这是功能代码:
Bitmap image = Bitmap.createBitmap(_view.getWidth(), _view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
android.graphics.drawable.Drawable bgDrawable = _view.getBackground();
if (bgDrawable!=null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
_view.draw(canvas);
java.io.File pictureFile = new java.io.File(Environment.getExternalStorageDirectory() + "/Phrasee/imagem.jpg");
if (pictureFile == null) {
return;
}
try {
java.io.FileOutputStream fos = new java.io.FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (java.io.FileNotFoundException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
} catch (java.io.IOException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
File imagePath = new File(Environment.getExternalStorageDirectory(), "/Phrasee/");
File newFile = new File(imagePath, "imagem.jpg");
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + ".provider", newFile);
File sdDir = new File(Environment.getExternalStorageDirectory() + "/Phrasee/imagem.jpg");
Intent iten = new Intent(android.content.Intent.ACTION_SEND);
iten.setType("image/jpeg");
iten.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
iten.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + ".provider", new java.io.File(Environment.getExternalStorageDirectory(), "/Phrasee/")));
startActivity(Intent.createChooser(iten, "Share with:"));
这就是清单中的提供者的样子:
<provider
android:authorities="gd.rf.tosproductions.phrasee.provider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
我还导入了androidx.core.content.FileProvider
显示可以共享图像的应用程序的对话框出现。但每当我选择 WhatsApp 或任何其他应用程序时,就会出现写有“共享失败,请重试”的祝酒词。
最后,我也已经设置了 provider_paths.xml。
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>
我真的不知道该怎么办。请给我一个可以解决的解决方案..
解决方案
推荐阅读
- matlab - Matlab MatConvNet vl_nnconv 函数中的语法错误实现为 Pytorch nn.conv1d 函数
- bash - ansible playbook 能够通过 mutt mail 发送邮件
- apache-kafka - 以循环方式消耗 100 个主题是一种反模式吗?
- python - Discord Bot 未提取嵌入式关键字
- php - 后备资源无法正常工作
- docker - 如何将配置文件传递给 docker 容器?
- python - 从列表中查找最大值并将其分配给饼图 MatPlotLib Python 中的爆炸值
- javascript - 我将如何使用 js 将十进制数转换为十六进制字符串?
- php - 如何获得用户拥有的徽章?
- nuxt.js - 登录后无法立即获取用户,但刷新工作@nuxt-auth