【问题标题】:Android - How to Share image using URI?Android - 如何使用 URI 共享图像?
【发布时间】:2019-11-12 10:13:00
【问题描述】:

我有图像 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)
    }

【问题讨论】:

    标签: android android-file android-fileprovider


    【解决方案1】:
    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!"));
    
    }
    

    【讨论】:

    • getExternalStorageDirectory 在 SDK 29 中已弃用。避免使用此
    • 但更糟糕的是没有使用FileProvider。
    猜你喜欢
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2020-05-05
    相关资源
    最近更新 更多