我已经尝试解决这个问题将近 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>
我真的不知道该怎么办。请给我一个可以解决的解决方案..