[英]Convert URL to URI and share to other apps
I have the URL to an image and I want to give the user the ability to share this images to another apps. 我有图片的URL,我想让用户能够将此图片分享给其他应用。 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. 当我尝试共享到WhatsApp时,出现“共享失败,请重试”,而对于Telegram,我得到“不支持的内容”,它无法与我选择的任何选项一起使用。
You can use share text
intent using the following methods. 您可以通过以下方法使用
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"));
Or with the ShareCompat
from support library. 或与支持库中的
ShareCompat
一起使用。
ShareCompat.IntentBuilder.from(activity)
.setType("text/plain")
.setChooserTitle("Share URL")
.setText(uri.toString())
.startChooser();
I managed to solve my own problem by using AsyncTask to convert the URL to an BitMap and then sharing to other apps. 通过使用AsyncTask将URL转换为BitMap,然后与其他应用共享,我设法解决了自己的问题。 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.