我有一个 fragment
,其中应该捕获图像并将其设置在imageview
中,然后将该图像发送到特定邮件。
我得到了第一部分,我可以在其中捕获图像并将其显示在 imageview
中。
但是通过邮件将该图像发送给其他人是行不通的。
请帮忙
最佳答案
使用这个
首先在imageview
上启用绘图缓存启用属性
final Bitmap bitmap = imageView.getDrawingCache();
on Button click to share do this
File dir = new File(Environment.getExternalStorageDirectory(), "AppNameFolder");
if (!dir.exists()) {
dir.mkdirs();
}
final File img = new File(dir, "image" + ".jpg");
if (img.exists()) {
img.delete();
}
final OutputStream outStream = new FileOutputStream(img);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
Uri photoURI = FileProvider.getUriForFile(context, getPackageName() + ".my.package.name.provider", img);
share.putExtra(Intent.EXTRA_STREAM, photoURI);
startActivity(Intent.createChooser(share, "Share image"));
注意:- 确保您还使用了文件提供程序和读/写权限
</application>
....
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="android.getqardio.com.gmslocationtest"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
其中 xml/provider_paths
是
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path
name="share"
path="external_files"/>
</paths>
关于java - 将 ImageView 中的图像附加到邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56784649/