1

I have the URL to an image and I want to give the user the ability to share this images to another apps. So I am using this method:

public void share() {
      if (mListener!=null){

        URI uri = null;
        try {
          URL url = new URL(mFile.getUrl()); //Some instantiated URL object
          uri = url.toURI();
          Intent shareIntent = new Intent();
          shareIntent.setAction(Intent.ACTION_SEND);
          shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
          shareIntent.setType("image/*");
          startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share)));

        } catch (MalformedURLException | URISyntaxException e) {
          Log.e("Sharing image", e.getMessage());
        }
      }
    }

When I try to share to WhatsApp I get "Sharing failed, please try again" and for Telegram I get "Unsupported content" it doesn't work with any of the options I get to choose from.

halfer
  • 19,824
  • 17
  • 99
  • 186
Marcos Guimaraes
  • 1,243
  • 4
  • 27
  • 50

2 Answers2

0

You can use share text intent using the following methods.

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Sharing URL");
i.putExtra(Intent.EXTRA_TEXT, uri.toString());
startActivity(Intent.createChooser(i, "Share URL"));

Or with the ShareCompat from support library.

ShareCompat.IntentBuilder.from(activity)
    .setType("text/plain")
    .setChooserTitle("Share URL")
    .setText(uri.toString())
    .startChooser();
Adib Faramarzi
  • 3,798
  • 3
  • 29
  • 44
  • Thanks for your reply. I tried your code but it's sharing the URL of the image. I would like to share the image itself. How can I do that? – Marcos Guimaraes Mar 26 '18 at 18:47
0

I managed to solve my own problem by using AsyncTask to convert the URL to an BitMap and then sharing to other apps. This is the code that I used:

public void share() {
      if (mListener!=null){

        new LongOperation().execute();
        progress = new ProgressDialog(getActivity());
        progress.setTitle(getActivity().getResources().getString(R.string.please_wait));
        progress.setCancelable(true);
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progress.show();
      }

    private class LongOperation extends AsyncTask<String, Void, String> {
      @Override
      protected String doInBackground(String... params) {

        Intent intent = new Intent(Intent.ACTION_SEND);
        //intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image");
        String path = Images.Media.insertImage(getActivity().getContentResolver(), getBitmapFromURL(mFile.getUrl()), "", null);
        Uri screenshotUri = Uri.parse(path);

        intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
        intent.setType("image/*");
        startActivity(Intent.createChooser(intent, "Share image via..."));
        progress.cancel();
        return null;
      }

      @Override
      protected void onPostExecute(String result) {
      }

      @Override
      protected void onPreExecute() {
      }

      @Override
      protected void onProgressUpdate(Void... values) {
      }
    }

    public static Bitmap getBitmapFromURL(String src) {
      try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
      } catch (IOException e) {
        // Log exception
        return null;
      }
    }
Marcos Guimaraes
  • 1,243
  • 4
  • 27
  • 50