将 URL 转换为 URI 并分享给其他应用

Convert URL to URI and share to other apps

我有一张图片的 URL,我想让用户能够将此图片分享到其他应用程序。所以我正在使用这种方法:

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());
        }
      }
    }

当我尝试分享到 WhatsApp 时,我得到 "Sharing failed, please try again",而对于 Telegram,我得到 "Unsupported content" 它不适用于我可以选择的任何选项。

您可以通过以下方法使用 share text 意图。

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

或使用支持库中的 ShareCompat

ShareCompat.IntentBuilder.from(activity)
    .setType("text/plain")
    .setChooserTitle("Share URL")
    .setText(uri.toString())
    .startChooser();

我设法通过使用 AsyncTask 将 URL 转换为 BitMap 然后共享给其他应用程序来解决我自己的问题。这是我使用的代码:

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;
      }
    }