Android Share Image From Url
Solution 1:
An adapted version of @eclass's answer which doesn't require use of an ImageView:
Use Picasso to load the url into a Bitmap
publicvoidshareItem(String url) {
Picasso.with(getApplicationContext()).load(url).into(newTarget() {
@OverridepublicvoidonBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Intent i = newIntent(Intent.ACTION_SEND);
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
startActivity(Intent.createChooser(i, "Share Image"));
}
@OverridepublicvoidonBitmapFailed(Drawable errorDrawable) { }
@OverridepublicvoidonPrepareLoad(Drawable placeHolderDrawable) { }
});
}
Convert Bitmap into Uri
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;
}
Solution 2:
I use these codes from this tutorial
final ImageView imgview= (ImageView)findViewById(R.id.feedImage1);
UribmpUri= getLocalBitmapUri(imgview);
if (bmpUri != null) {
// Construct a ShareIntent with link to imageIntentshareIntent=newIntent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share Image"));
} else {
// ...sharing failed, handle error
}
then add this to your activity
public Uri getLocalBitmapUri(ImageView imageView) {
// Extract Bitmap from ImageView drawableDrawabledrawable= imageView.getDrawable();
Bitmapbmp=null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
returnnull;
}
// Store image to default external storage directoryUribmpUri=null;
try {
Filefile=newFile(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStreamout=newFileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
then add your application manifest
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE" />
Solution 3:
You need to use a local file. Like this:
UriimageUri= Uri.parse("android.resource://your.package/drawable/fileName");
Intentintent=newIntent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(intent , "Share"));
If your image is on remote server download it to the device first.
Solution 4:
Try this:
newOmegaIntentBuilder(context)
.share()
.filesUrls("http://stacktoheap.com/images/stackoverflow.png")
.download(new DownloadCallback() {
@Override
public void onDownloaded(boolean success, @NotNull ContextIntentHandler contextIntentHandler) {
contextIntentHandler.startActivity();
}
});
Solution 5:
After a lot of pondering, here is the code that I found to be working for me! I think this is one of the simplest versions of code to achieve the given task using Picasso. There is no need to create an ImageView object.
Target target = newTarget() {
@OverridepublicvoidonBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
bmp = bitmap;
String path = MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "SomeText", null);
Log.d("Path", path);
Intent intent = newIntent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image");
Uri screenshotUri = Uri.parse(path);
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share image via..."));
}
@OverridepublicvoidonBitmapFailed(Drawable errorDrawable) {
}
@OverridepublicvoidonPrepareLoad(Drawable placeHolderDrawable) {
}
};
String url = "http://efdreams.com/data_images/dreams/face/face-03.jpg";
Picasso.with(getApplicationContext()).load(url).into(target);
Here, bmp is a class level Bitmap variable and url will be the dynamic Internet url to your image for sharing. Also, instead of keeping the code to share inside the onBitmapLoaded() function, it can also be kept inside another handler function and later called from the onBitmapLoaded() function. Hope this helps!
Post a Comment for "Android Share Image From Url"