Rohan Suri
Rohan Suri

Reputation: 111

How do I share images from Firebase in a RecyclerView?

This is my share intent in my MainActivity class. I am getting an error at Uri.parse line.

     viewHolder.mShareButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent shareIntent = new Intent();
              shareIntent.setAction(Intent.ACTION_SEND);
              shareIntent.putExtra(Intent.EXTRA_TEXT, "Shared via Entrepreneur Quotebook");
              shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(getImageUri(Context ctx,)));

              shareIntent.setType("image/*");
              startActivity(Intent.createChooser(shareIntent, "Share image via:"));

This method is used to parse the Uri. I am not sure about this method to be correct as I have copied this from somewhere.

    public Uri getImageUri(Context ctx, Bitmap post_image) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    post_image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(ctx.getContentResolver(), post_image, "Title", null);
    return Uri.parse(path);

Upvotes: 0

Views: 655

Answers (1)

Nishant Thapliyal
Nishant Thapliyal

Reputation: 1670

Okay, so try this:-

public class RecycleClass extends RecyclerView.Adapter<RecycleClass.ViewHolder> { 
    Context c;
    ArrayList<String> yourData = new ArrayList<>();//additional parameters



    RecycleClass(Context c,ArrayList<String> yourData){
      this.c = c; //this is important
      this.yourData = yourData;
    }

    /*
      Rest of your code...
     */
  }

And from your main activity (or the activity where you are setting up this recyclerview)

//this will setup your context
adapter = new RecycleClass(MainActivity.this,yourData);

And also replace this

startActivity(Intent.createChooser(shareIntent, "Share image via:"));

with

c.startActivity(Intent.createChooser(shareIntent, "Share image via:"));

And finally

public Uri getImageUri(Bitmap post_image) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
post_image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(c.getContentResolver(), post_image, "Title", null);
return Uri.parse(path);
}

Now, where ever you are calling your getImageUri() method just pass Bitmap and not the context.

Try this and let me know if its working or not.

Upvotes: 1

Related Questions