将URL转换为URI并共享给其他应用程序

时间:2018-03-26 18:30:44

标签: android share

我有图片的网址,我想让用户能够将这些图片分享给其他应用。所以我使用这种方法:

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分享时,我收到“分享失败,请再试一次”,而对于电报我得到“不支持的内容”,它不适用于我可以选择的任何选项。

2 个答案:

答案 0 :(得分:0)

您可以使用以下方法使用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();

答案 1 :(得分:0)

我设法通过使用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;
      }
    }