android - Android - 如何使用 URI 共享图像?
问题描述
我有图像 uri,我正在尝试使用下面的代码共享它,但它不起作用。
共享意图已打开,但那里没有图像。请在这件事上给予我帮助
private fun ivShareClickListener(position: Int) {
var file = File(dataList[position].uri.path)
val sharingIntent = Intent(Intent.ACTION_SEND)
//sharingIntent.type = "*/*"
val apkURI = FileProvider.getUriForFile(
context, context.applicationContext.packageName + ".provider", file
)
sharingIntent.setDataAndType(apkURI, "*/*")
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(context, Intent.createChooser(sharingIntent, "Share image using"), null)
}
解决方案
private void shareImage() {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
String imagePath = Environment.getExternalStorageDirectory() + "/myImage.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}
推荐阅读
- mysql - 警告:(1366,“第 1 行的列 'VARIABLE_VALUE' 的字符串值不正确:'\\xB4\\xEB\\xC7\\xD1\\xB9\\xCE...'”)结果 = self._query(查询)
- python - 分组和聚合后未找到 Pandas colnames
- android - 如何在不以图形方式剪切的情况下仅将前一个片段滑出 50% 或 25%?
- java - 如何在 MapStruct 中映射扩展类
- python - 从两个字典的差异创建字典
- node.js - 属性 id 后的 MongoDb Shell 语法错误
- javascript - 为什么 vuelidate 验证器不再对输入更改做出反应?
- java - 在 Android 中的活动之间保持 Websocket 活动
- c# - 将字符串值转换为浮点值
- php - IIS Plesk - 将 PHP 的 FastCGI 协议从命名管道更改为 TCP