webcare infoway
webcare infoway

Reputation: 31

How to share an image which is load from the server using Picasso?

I need help.
I've already fetched an image from the server using web-services, but I don't share this image.

I attached my code, please help me to find the error.

share.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Bitmap bitmap = viewToBitmap(iv, iv.getWidth(), iv.getHeight());
        Intent shareintent = new Intent(Intent.ACTION_SEND);
        shareintent.setType("image/jpeg");
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        File file = new File(Environment.getExternalStorageDirectory() + 
                             File.separator + "Imagedemo.jpg");
        try {
            file.createNewFile();
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(byteArrayOutputStream.toByteArray());
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        shareintent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/Imagedemo.jpg"));

        startActivity(Intent.createChooser(shareintent,"share image"));
    }
}); 

public static Bitmap viewToBitmap(View view, int width, int height){

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;    
}

Upvotes: 3

Views: 2390

Answers (2)

Geo
Geo

Reputation: 756

Load image using url to Image view is simple

Picasso.get().load(imageUrl).into(imageView);

To share image from imageView using share button use code below

share.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageVIew.getDrawable());
      Bitmap bitmap = bitmapDrawable .getBitmap();
      String bitmapPath = Images.Media.insertImage(getContentResolver(), bitmap,"some 
      title", null);
      Uri bitmapUri = Uri.parse(bitmapPath);
      Intent shareIntent=new Intent(Intent.ACTION_SEND);
      shareIntent.setType("image/jpeg");
      shareIntent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
      startActivity(Intent.createChooser(shareIntent,"Share Image"));
  }
}

Upvotes: 9

Hemant Parmar
Hemant Parmar

Reputation: 3976

Change this line

shareintent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/Imagedemo.jpg"));

To:

shareintent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Imagedemo.jpg"));

or better way to get file from external storage is:

new File(Environment.getExternalStorageDirectory() + "/" + "Imagedemo.jpg")

one more thing setType for jpg image is

 shareintent.setType("image/jpg");

Upvotes: 0

Related Questions