首页 > 解决方案 > 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)
    }

标签: androidandroid-fileandroid-fileprovider

解决方案


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!"));

}

推荐阅读