【问题标题】:Share Image (from URL) along with text with Android Intent与 Android Intent 共享图像(来自 URL)和文本
【发布时间】:2020-11-24 05:15:06
【问题描述】:

我正在从服务器获取不同文章的动态图像和文本。

我需要向用户提供分享选项,以便他可以在所需的社交媒体平台(如 facebook、instagram、whatsapp 等)上分享图像和文本。

其他帖子中提供的解决方案都不适合我。他们将图像保存在本地(这对我也不起作用)。

任何帮助将不胜感激!

【问题讨论】:

标签: android android-intent share android-sdk-tools


【解决方案1】:

了解想法并根据您的方案实施

Intent shareIntent;
    Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/Share.png";
    OutputStream out = null;
    File file=new File(path);
    try {
        out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    path=file.getPath();
    Uri bmpUri = Uri.parse("file://"+path);
    shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
    shareIntent.putExtra(Intent.EXTRA_TEXT,"Hey please check this application " + "https://play.google.com/store/apps/details?id=" +getPackageName());
    shareIntent.setType("image/png");
    startActivity(Intent.createChooser(shareIntent,"Share with"));

【讨论】:

    【解决方案2】:

    使用任何从图像链接中提供位图的图像库 (毕加索,滑翔,线圈**,..)

    fun shareImageFromURI(url: String?) {
              Picasso.get().load(url).into(object : Target {
                  override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
                      val intent = Intent(Intent.ACTION_SEND)
                      intent.type = "image/*"
                      intent.putExtra(Intent.EXTRA_TEXT, "Your awesome text");
                      intent.putExtra(Intent.EXTRA_STREAM, getBitmapFromView(bitmap))
                      startActivity(Intent.createChooser(intent, "Share Image"))
                  }
                  override fun onPrepareLoad(placeHolderDrawable: Drawable?) { }
                  override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) { }
              })
        }
    

    点击分享按钮,你可以调用这个函数并将图片链接传递给它,下载图片后会启动intent

    更新

     fun getBitmapFromView(bitmap:Bitmap):Uri{
        val bitmapPath = Images.Media.insertImage(getContentResolver(), 
        bitmap,"title", null);
        return Uri.parse(bitmapPath);
        }
    

    【讨论】:

    • getBitmapFromView(bitmap) 方法的主体内容是什么?
    • 函数给出位图的URI
    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多