Not able to share an Image on Whatsap - android

I am writing an app to share image.
I wrote the code but getting problems.
What I am doing
1: Saving the image temporarily in Internal Stoarge
2: Passing the URI to share the image.
Image is successfully saved in internal storage but not getting share on whatsapp.
When I share it on whatsapp, Whatsapp get opens, I select recipient, whatsapp processes it and says "Retry".
Code:
public void shareImage(View V)
{
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.download);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "myDownloadedImage.jpg");
try
{
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
Toast.makeText(this, "Some error in Writing"+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Uri downloadLocation=Uri.fromFile(file);
share.putExtra(Intent.EXTRA_STREAM, downloadLocation);
startActivity(Intent.createChooser(share, "Share Image"));
}
Image succesfully get saved on internal storage but not getting shared on whatsapp.
The screenshot is below. We can see images are not shared successfully.

Use below code to fetch image and make uri:
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
Now pass the bmpUri in:
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
This should make your image share.

Related

How to share images that are not stored on device?

I have a application where i convert the text coming from user into a QR code and display the QR code in imageView. My problem is how can i use sharing Intent on this image as their no path specified for it nor the image is stored anywhere ?
You can do it by saving the bitmap of image in cache followed by sharing it.
//To get the bitmap from Imageview
imageView.setDrawingCacheEnabled(true);
Bitmap bitmap = imageView.getDrawingCache();
//To share it via Intent**
try {
File file = new File(getContext().getCacheDir(), fileName + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}

Share Image from url

I am tring to share an image which i fetch from Json. After a number of attempts i finally concluded with this code. It works well till i open whatsapp and select the contact i want to share, the image doesnt load from there and when i click on submit, it says sharing failed.
Drawable drawable1 = imageViewPreview.getDrawable();
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.id.image_preview);
File file = new File(SlideshowDialogFragment.this.getCacheDir(), String.valueOf(largeIcon + ".png"));
// FileOutputStream fOut = new FileOutputStream(file);
// largeIcon.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
file.setReadable(true, false);
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
startActivity(intent);
if i remove the comments
FileOutputStream fOut = new FileOutputStream(file);
will show error message and i have to add try/catch then, the app crashes saying Nullpoint exception
Try this code:
Bitmap bitmap = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "photo.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/photo.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));

Share Image in android

I need to share an image (.png format) with transparent background, through sent_action intent.
I searched a lot and tried a lot of samples but couldn't find the solution.
The is this method in witch the image will get shared directly from the resources, but for some reason from some point it has stop working.
Uri url = Uri.parse("android.resource://"
+ getPackageName() + "/" + R.drawable.ic_launcher);
Intent share_intent = new Intent();
share_intent.setAction(Intent.ACTION_SEND);
share_intent.setType("image/png");
share_intent.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(new File(url.toString())));
startActivity(Intent.createChooser(share_intent, "choose app"));
and there is this function which works fine, the problem is it adds a black background to the image.
private void share3()
{
Bitmap bitmap;
OutputStream output;
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath() + "/Gallery/");
dir.mkdirs();
File file = new File(dir, "ic_launcher" + ".png");
try {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
output = new FileOutputStream(file);
bitmap. compress(Bitmap.CompressFormat.PNG/*Bitmap.CompressFormat.PNG*/, 0, output);
output.flush();
output.close();
Uri uri = Uri.fromFile(file);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "choose app"));
} catch (Exception e) {
e.printStackTrace();
}
}
I need to share the image from "raw" folder in the resources and share it without background. What should I do?
I found the problem, It is telegram itself. Telegram adds a black background to .png images you share.
Try this ..its work for me to share image from drawable folder.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/LatestShare.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);
Intent shareIntent = new Intent();
shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent, "Share with"));

Saving image file and returning to whatsapp

I've used the following code to save an image file to the external storage
drawView.setDrawingCacheEnabled(true);
String img = MediaStore.Images.Media.insertImage(getContentResolver(),drawView.getDrawingCache(),UUID.randomUUID().toString()+".jpeg", "doodle");
I want to return this saved file to Whatsapp using an intent. Can somebody please help me out with the code? I'm new to android and would really appreciate it! Thanks :)
Try this code
Bitmap icon = mBitmap;
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_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));

Can't attach bitmap image. Android

I saw other questions about this, tried their answers, but don't work.
I am trying to use the second method from developer.android.com.
(the second white bullet).
I save an image to applications internal memory, with appropriate permissions.
The image (bitmap) is saved successfully, but I can't attach it to a new intent, to share it.
Here is my code:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("*/*");//Attach all types (images/text)
FileOutputStream fos;
try{
fos = openFileOutput(myfilename, Context.MODE_WORLD_READABLE);
mybitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
//Image is stored successfully (checked data/data/mypackage/files/myfilename)
Uri uri = Uri.fromFile(getFileStreamPath(myfilename));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
}
catch (Exception e){
// noth
//Log.e(TAG, e.getStackTrace().toString());
}
shareIntent.putExtra(Intent.EXTRA_TEXT, "some text also");
return shareIntent;
}
Applications say that can't attach media items.
Gmail seems to attach item, but it shows nothing when mail send!
Also the uri.getPath returns me:
/data/data/mypackage/files/myfilename,
which is correct
Any ideas?
Thank you!
Edit:
Modified code to use sd card. And still don't get it to work:
File imgDir;
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
imgDir = new File(
android.os.Environment.getExternalStorageDirectory(),
".MyAppName/Images");
else imgDir = MyActivity.this.getCacheDir();
if (!imgDir.exists()) imgDir.mkdirs();
File output = new File(imgDir, myFilename);
OutputStream imageFileOS;
try{
Uri uriSavedImage = Uri.fromFile(output);
imageFileOS = getContentResolver().openOutputStream(
uriSavedImage);
bitmapBookCover.compress(Bitmap.CompressFormat.PNG, 90,
imageFileOS);
imageFileOS.flush();
imageFileOS.close();
//Again image successfully saved
shareIntent.putExtra(Intent.EXTRA_STREAM, uriSavedImage);
}
catch (Exception e){
// noth
}
Please try usiing setData() instead of putExtra(), here android developer site - intent set data

Categories

Resources