Share image and text with Intent to Whats App - android

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

Related

Image only Shared with Whatsapp but not with other apps in Android

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

Android Bitmap to File send with Intent

I have this code that gets an image from database as Bitmap and then writes this to a file and sends it with e-mail. This code works great.
I am trying to write this to a textfile that actually shows the picture.
Is this possible?
Do I need to write this to pdf file if I want a file that shows the image?
Here's my code
public void createBild(long x, String pathToFile, String fileName) {
Product product = dbHandler.findProductbyId(x);
Bitmap pic = BitmapFactory.decodeByteArray(dbHandler.fetchSingle(x), 0,
dbHandler.fetchSingle(x).length);
// create a file to write bitmap data
Bitmap bitmap = pic;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
byte[] bitmapdata = bos.toByteArray();
File f = new File(pathToFile + "/"+fileName+".bmp");
try {
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Uri path = Uri.fromFile(f);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/*");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "test#live.se" });
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT, "body of email");
i.putExtra(Intent.EXTRA_STREAM, path);
try {
startActivity(Intent.createChooser(i, "Share"));
} catch (android.content.ActivityNotFoundException e) {
Toast.makeText(VisaData.this,
"There are no email clients installed.", Toast.LENGTH_SHORT)
.show();
}
}
Why don't you send it as a .png itself.
intent.setType("image/png");

How can I send bitmap to WhatsApp Application

My question how can I send bitmap to Whastapp Application and I use below code;
ImageView iv=(ImageView)view.findViewById(R.id.item_image);
Bitmap bitmap = ((BitmapDrawable)iv.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.setType("image/png");
waIntent.putExtra(Intent.ACTION_SEND, byteArray);
startActivity(Intent.createChooser(waIntent, "Share with"));
But that code did not work. What is my error ? Thanks.
This worked for me:
public void onClickApp(String pack, Bitmap bitmap) {
PackageManager pm = context.getPackageManager();
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "Title", null);
Uri imageUri = Uri.parse(path);
#SuppressWarnings("unused")
PackageInfo info = pm.getPackageInfo(pack, PackageManager.GET_META_DATA);
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("image/*");
waIntent.setPackage(pack);
waIntent.putExtra(android.content.Intent.EXTRA_STREAM, imageUri);
waIntent.putExtra(Intent.EXTRA_TEXT, pack);
context.startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (Exception e) {
Log.e("Error on sharing", e + " ");
Toast.makeText(context, "App not Installed", Toast.LENGTH_SHORT).show();
}
}
//pass your image and text(if you want to share) in this method.
void shareImage(Bitmap bitmap,String text){
//bitmap is ur image and text is which is written in edtitext
//you will get the image from the path
String pathofBmp=
MediaStore.Images.Media.insertImage(getContentResolver(),
bitmap,"title", null);
Uri uri = Uri.parse(pathofBmp);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Star App");
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "hello hello"));
}

Android save and share Image From SDCard

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" />

problem in taking screenshots in android by programmatically

in my app i am taking screenshot of the current screen and save it in sdcard. but in the case screenshot is not saved in sdcard. how to take screen shot and send the captured screen shot in email as attachment. please help me.
my coding:
View v1 = view.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
try
{
System.out.println("path "+Environment.getExternalStorageDirectory());
FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory()+"/ff");
bm.compress(CompressFormat.PNG, 90, out);
}
catch (Exception e)
{
e.printStackTrace();
}
Intent emailIntent = new Intent(Intent.ACTION_SEND);
Uri U=Uri.parse("file:///sdcard/ff");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "aabc#gmail.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, " from ..");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "from the app");
emailIntent.setType("image/png");
// emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,U);
emailIntent.putExtra(Intent.EXTRA_STREAM, U);
startActivity(Intent.createChooser(emailIntent, ""));
please help me.
i solved my problem by replace the try clause coding by
File file = new File(Environment.getExternalStorageDirectory()+"/filmfestival.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}

Categories

Resources