0

I tried sharing the images from my app to other apps but a toast is displayed = "file not sent". not sure what to do. down below I have posted the code that I am using to share image

 @Override
    public void onWhatEverClick(int position) {
        Toast.makeText(this, "Normal click at position: " + position, Toast.LENGTH_SHORT).show();
        try {
            Upload selectedItem = mUploads.get(position);

            final String selectedkey = selectedItem.getKey();
            StorageReference imgRef = mStorage.getReferenceFromUrl(selectedItem.getImageUrl());
            String url = selectedItem.getImageUrl();
            String imageString = url.toString();
            URI uri = new URI(imageString);
            final Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("image/jpg");
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            startActivity(Intent.createChooser(shareIntent, "Share image using"));
        }


        catch (Exception e)
        {

        }
    }

1 Answers1

0

You can't directly share the remote image using just it's url, you'll need to download it before sharing.

The 'easy' way of doing this would be to use a library like Picasso or Glide to download the file into a bitmap, store it in the ExternalFiles and get the URI from the file which can then be shared.

public void shareItem(String url) {
    Picasso.with(getApplicationContext()).load(url).into(new Target() {
        @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("image/*");
            i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
            startActivity(Intent.createChooser(i, "Share Image"));
        }
        @Override public void onBitmapFailed(Drawable errorDrawable) { }
        @Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
    });
}

public Uri getLocalBitmapUri(Bitmap bmp) {
    Uri bmpUri = null;
    try {
        File file =  new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}

Example code referenced from this stackoverflow answer.

Chrisvin Jem
  • 3,940
  • 1
  • 8
  • 24