ajay
ajay

Reputation: 916

Share Image (from URL) along with text with Android Intent

I am getting a dynamic image and text from server for different articles.

I need to provide share option to the user, so he can share both image and text on desired social media platforms like facebook, instagram, whatsapp, etc.

None of the solution provided in other posts is working for me. They were keeping images locally (which is also not working for me).

Any help would be much appreciated!!

Upvotes: 0

Views: 2092

Answers (2)

d-feverx
d-feverx

Reputation: 1672

use any image library that gives bitmap from an image link (Picasso, Glide, Coil**,..)

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?) { }
          })
    }

on click of the share button, you can call this function and pass the image link to it, it will start intent after downloaded the image

update

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

Upvotes: 1

Muhammad Humza Khan
Muhammad Humza Khan

Reputation: 271

Get the idea and implement it as per your scenario

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

Upvotes: 3

Related Questions