I am trying to share an image from imageview with text caption to whatsApp but the solutions I found online didn't seem to work for me.
View content = findViewById(R.id.posted_house);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try
{
root.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
Intent txtIntent = new Intent(android.content.Intent.ACTION_SEND);
txtIntent .setType("image/*");
txtIntent .putExtra("message");
txtIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
startActivity(Intent.createChooser(txtIntent ,"Share"));
}
Here is code for share image and text in whatsapp..
View screenView = rootView.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), bitmap, "Title", null);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "Your message");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));
try {
startActivity(Intent.createChooser(intent, "Share Screenshot"));
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No App Available", Toast.LENGTH_SHORT).show();
}
I am trying to share image using intent. Here is the method that i created
public void shareImg(int fileNum) //Consider fileNum=R.drawable.img
{
Uri uri= Uri.parse("android.resource://"
+ context.getPackageName() + "/" + fileNum);
Intent share=new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TEXT, "Sent Via ---");
Intent chooser= Intent.createChooser(share, "Share Via");
context.startActivity(chooser);
}
The image is shared properly with Whatsapp with caption. But when I try to share app with Gmail, Messenger, etc it gives error shown in Toast.
For eg.
Gmail says : Can't attach empty file
Messenger says : Failed to convert to image
You can share image using share intent, but you've to decode image to a localized Bitmap
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image");
String path = Images.Media.insertImage(getContentResolver(), loadedImage, "", null);
Uri screenshotUri = Uri.parse(path);
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share image via..."));
loadedImage is the Path of image
Here is the steps you can perform.
Step 1. First create bitmap from drawable
Drawable d = ImagesArrayList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
Step 2. Save Bitmap to File
FileOutputStream out = null;
String filename = Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg";
try {
out = new FileOutputStream(filename);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Step 3. Share this image File with fileurl. Share the Image same as you sharing gallery image.
Complete Answer
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.xxxx); // your resource ID here
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/LatestShare.jpg";
OutputStream out = null;
File file=new File(path);
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
path=file.getPath();
Uri bmpUri = Uri.parse("file://"+path);
Intent shareIntent = new Intent();
shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/jpg");
startActivity(Intent.createChooser(shareIntent,"Share with"));
Hi I'm Trying to save a drawable as a bitmap to the SDCard and then share it using a share intent. but the problem i'm having is that its just not sharing the image does anyone know how to do this or where im going wrong?
heres what i have tried so far
getHelp.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
saveBitmapToExternalStorage(this, imageResourceFor Drawable, imageName);
File imageFile = new File("/sdcard/myfolder/"+ kitImagePath+".png");
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Title");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Subtitle");
shareIntent.putExtra(Intent.EXTRA_STREAM, bitmap); //optional//use this when you want to send an image
shareIntent.setType("image/png");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
}
});
public static void saveBitmapToExternalStorage(Context context, int imageResource, String imageName){
Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(),imageResource);
//generate file
File dir = new File (Environment.getExternalStorageDirectory(), "/myfolder");
File f = new File(dir, String.format(imageName + ".png"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 , bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos;
try {
fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Instead of using Bitmap use its URI to share.
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Title");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Subtitle");
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+ kitImagePath+".png"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri); //optional//use this when you want to send an image
shareIntent.setType("image/png");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
Also as +Der Golem said. use Environment.getExternalStorageDirectory().getAbsolutePath() to get External storage path.
Add the following Permission in your manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I used ShareIntent in android .i want share bitmap image in facebook via shareintent.my code below . how to slove this question.
Thanks!!!
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Facebook") {
Uri pngUri = Uri.parse("file//res/drawable/camera.png");
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"YOUR TEXT HERE");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,"YOUR TEXT HERE");
shareIntent.putExtra(android.content.Intent.EXTRA_STREAM,pngUri);
PackageManager pm =getActivity().getApplicationContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("facebook")) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(
activity.applicationInfo.packageName,
activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
startActivity(shareIntent);
}
}
}
else if (item.getTitle() == "Twitter") {
Toast.makeText(getActivity().getApplicationContext(), "Twitter",
1).show();
} else {
return false;
}
return true;
}
try this
//add code on save button
File file;
Bitmap bitmap=((BitmapDrawable)imageview.getDrawable()).getBitmap();
ByteArrayOutputStream stream1=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream1);
byte[] imageInByte2=stream1.toByteArray();
try{
String path = Environment.getExternalStorageDirectory().toString();
File imgDirectory = new File(Environment.getExternalStorageDirectory()+"/Cards/");
imgDirectory.mkdirs();
OutputStream fOut = null;
file = null;
file = new File(path,"/Cards/"+etcardname.getText().toString()+ ".png");
Toast.makeText(getActivity(), "saved at: " + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
}catch(Exception e){
e.printStackTrace();
}
//share image code
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file));
startActivity(Intent.createChooser(share, "Share image using"));
I want to share Text + Image together using ACTION_SEND in android, I am using below code, I can share only Image but i can not share Text with it,
private Uri imageUri;
private Intent intent;
imageUri = Uri.parse("android.resource://" + getPackageName()+ "/drawable/" + "ic_launcher");
intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
intent.setType("image/*");
startActivity(intent);
Any help on this ?
You can share plain text with the following code
String shareBody = "Here is the share content body";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));
So your full code (your image + text) becomes
private Uri imageUri;
private Intent intent;
imageUri = Uri.parse("android.resource://" + getPackageName() +
"/drawable/" + "ic_launcher");
intent = new Intent(Intent.ACTION_SEND);
// text
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
//image
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
//type of content
intent.setType("*/*");
//sending
startActivity(intent);
I just replaced image/* with */*
Update:
Uri imageUri = Uri.parse("android.resource://" + getPackageName() +
"/drawable/" + "ic_launcher");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello");
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
please have a look on this code worked for me to share an text and image together
Intent shareIntent;
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/Share.png";
OutputStream out = null;
File file=new File(path);
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
path=file.getPath();
Uri bmpUri = Uri.parse("file://"+path);
shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.putExtra(Intent.EXTRA_TEXT,"Hey please check this application " + "https://play.google.com/store/apps/details?id=" +getPackageName());
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent,"Share with"));
Don't forget to give WRITE_EXTERNAL_STORAGE Permission
also in facebook it can only share the image because facebook is not allowing to share the text via intent
It is possibly because the sharing app (FB, twitter, etc) may not have permissions to read the image.
Google's document says:
The receiving application needs permission to access the data the Uri points to. The recommended ways to do this are:
http://developer.android.com/training/sharing/send.html
I am not sure the sharing apps have permissions to read an image in the bundle of our apps. But my files saved in
Activity.getFilesDir()
cannot be read. As suggested in the above link, we may consider to store images in the MediaStore, where the sharing apps have permissions to read.
Update1:
The following is working code to share image with text in Android 4.4.
Bitmap bm = BitmapFactory.decodeFile(file.getPath());
intent.putExtra(Intent.EXTRA_TEXT, Constant.SHARE_MESSAGE
+ Constant.SHARE_URL);
String url= MediaStore.Images.Media.insertImage(this.getContentResolver(), bm, "title", "description");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share Image"));
The side effect is we add an image in the MediaStore.
I have been looking for solution of this question for a while and found this one up and running, hope it helps.
private BitmapDrawable bitmapDrawable;
private Bitmap bitmap1;
//write this code in your share button or function
bitmapDrawable = (BitmapDrawable) mImageView.getDrawable();// get the from imageview or use your drawable from drawable folder
bitmap1 = bitmapDrawable.getBitmap();
String imgBitmapPath= MediaStore.Images.Media.insertImage(getContentResolver(),bitmap1,"title",null);
Uri imgBitmapUri=Uri.parse(imgBitmapPath);
String shareText="Share image and text";
Intent shareIntent=new Intent(Intent.ACTION_SEND);
shareIntent.setType("*/*");
shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareText);
startActivity(Intent.createChooser(shareIntent,"Share Wallpaper using"));
String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", null);
Uri bitmapUri = Uri.parse(bitmapPath);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
intent.putExtra(Intent.EXTRA_TEXT, "This is a playstore link to download.. " + "https://play.google.com/store/apps/details?id=" + getPackageName());
startActivity(Intent.createChooser(intent, "Share"));
try using this code.I am uploading ic_launcher from drawable.you can change this with your file from gallary or bitmap.
void share() {
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_TEXT, "hello #test");
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
}
String text = "Look at my awesome picture";
Uri pictureUri = Uri.parse("file://my_picture");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share images..."));
Check the below code it worked for me
Picasso.with(getApplicationContext()).load("image url path").into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_TEXT, "Let me recommend you this application" +
"\n"+ "your share url or text ");
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
context.startActivity(Intent.createChooser(i, "Share using"));
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
private Uri getLocalBitmapUri(Bitmap bmp) {
Uri bmpUri = null;
try {
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 50, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
private void shareImage(){
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.Starlay_straightface_image);
File f = new File(getExternalCacheDir()+"/"+getResources().getString(R.string.app_name)+".png");
Intent shareIntent;
try {
FileOutputStream outputStream = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
outputStream.flush();
outputStream.close();
shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
shareIntent.putExtra(Intent.EXTRA_TEXT,"Hey please check this application " + "https://play.google.com/store/apps/details?id=" +getPackageName());
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}catch (Exception e){
throw new RuntimeException(e);
}
startActivity(Intent.createChooser(shareIntent,"Share Picture"));
}
To share a drawable image, the image has to be first saved in device's cache or external storage.
We check if "sharable_image.jpg" already exists in cache, if exists, the path is retrieved from existing file.
Else, the drawable image is saved in cache.
The saved image is then shared using intent.
private void share() {
int sharableImage = R.drawable.person2;
Bitmap bitmap= BitmapFactory.decodeResource(getResources(), sharableImage);
String path = getExternalCacheDir()+"/sharable_image.jpg";
java.io.OutputStream out;
java.io.File file = new java.io.File(path);
if(!file.exists()) {
try {
out = new java.io.FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
path = file.getPath();
Uri bmpUri = Uri.parse("file://" + path);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_TEXT, "This is a sample body with more detailed description");
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent,"Share with"));
}
I tried this solution with Android X and works perfectly with whatsapp, telegram and gmail.
This is my code:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "<<sharingText>>");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("<<imageResource>>"));
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (getView() != null && getView().getContext() != null &&
shareIntent.resolveActivity(getView().getContext().getPackageManager()) != null) {
getView().getContext().startActivity(Intent.createChooser(shareIntent, null));
}
By accident(the text message part, I had given up on that), I noticed that when I chose Messages App to handle the request, the Message App would open with the text from Intent.EXTRA_SUBJECT plus the image ready to send, I hope it helps.
String[] recipient = {"your_email_here"};
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipient);
intent.putExtra(Intent.EXTRA_SUBJECT, "Hello, this is the subject line");
intent.putExtra(Intent.EXTRA_TEXT, messageEditText.getText().toString());
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
intent.setType("image/*");
You can this code it is working
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.refer_image);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*" + "text/*");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(this.getContentResolver(),
b, "Title", null);
Uri imageUri = Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
share.putExtra(Intent.EXTRA_TEXT, "Hey I am Subhajit");
startActivity(Intent.createChooser(share, "Share via"));
File Provider:
it is good to use FileProvider to share image to other app securely.
it work on all version of android.
i tried it and works fine.
Link:
https://developer.android.com/reference/androidx/core/content/FileProvider