Send Image via Intent by WhatsApp - Wrong Image - android

I thought that this is a popular problem, but I can't find anything related to it. So here is what I want:
I'm trying to send an Image by WhatsApp with the following code:
public static void shareImage(Context context,Bitmap bitmap, String text){
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.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());
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
share.setPackage("com.whatsapp");
context.startActivity(Intent.createChooser(share, "Share!"));
}
It works fine, for the first time I use the app.. the right image appear in WhatsApp. If I choose another Bitmap with my App, Whatsapp still shows the first Image.
What I did and what I thought caused the problem:
If the temporary file existed, I deleted it by f.delete()
Then I updated the Gallery with MediaScanner, because I thought, that the URI I put in Extra is not up to date...
Fyi: The temporary file contains the right Image: If I choose it with an FileExplorer it shows the right image.. if I try to send the Image.. still the old Image
Does anyone has an idea what the problem might be? Is the Uri wrong? If I print Uri.fromFile(f)it says file:///storage/sdcard0/temporary_file.jpg
Thank you!
Nico

Did you actually send the first image or did you cancel it? I have the same problem and i just realized that the correct image will be send if you continue. I guess the problem is caused by old thumbnail pictures. You could use different filenames.
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file"+ System.currentTimeMillis() +".jpg");
Later you can delete all files starting with "temporary_file"
for (File file : Environment.getExternalStorageDirectory().listFiles()) {
if (file.isFile() && file.getName().startsWith("temporary_file")) file.delete();
}

Related

Share content to Messenger or Line or other like Facebook or Tweeter

Hi I am new to develop application in android.
I saw other apps, they use share to friends with Messenger, or Facebook..
for instant, I share it to Messenger, then in the content should show some texts and the link of app!!
How can I do that? anyone please help!!!
I have tried the answer in this site to share, but it works only with SMS and Gmail, but if I share to Messenger, it shows nothing!
Try this code for sharing drawable image with link and text.
Follow this steps
Step 1 :- Take one button in your layout.xml for sharing your content.
Step 2 :- set onClickListner for this button in your Activity.java.
Step 3 :- Put this code to your Activity.java file Button setOnClickListner.
String myText = "share text though this string";
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.mipmap.icon_256);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpg");
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();
}
String extraText = myText + "\n\nhttps://www.google.co.in";
share.putExtra(Intent.EXTRA_TEXT,extraText);
share.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(f));
startActivity(Intent.createChooser(share, "Share Image"));

Android: Saving a bitmap Image

I have a button that allows the user to save a bitmap image to his/her gallery in a seperate folder after applying modifications on some of pixel bits as shown below
Random rand= new Random(TimeZone.LONG);
File root=null;
File file = null;
try{
root= Environment.getExternalStorageDirectory();
file = new File(String.format("%s/IMG_%d.png", root.getCanonicalPath(), Math.abs(rand.nextInt()%100000)));
FileOutputStream out = new FileOutputStream(file);
btmImg.compress(Bitmap.CompressFormat.PNG, 100, out);
MediaStore.Images.Media.insertImage(getContentResolver(), file.getCanonicalPath(), file.getName(), file.getName());
out.flush();
out.close();
Toast.makeText(getApplicationContext(), "File is Saved in " + file, 2000).show();
}
The problem is that when I press 'save' button. The image that appears in the user's gallery is not the modified version it is a duplication of the original or something like that. To get the modified version I had to refresh the gallery (by deleting the applications running in the background).
Anyone has an idea about how to solve this !!

Android: write photo files on SD Card that are viewable in the gallery

I'm trying to found a way (compatible with android kitkat and next) to write photos on the SD Card and make them visible to the gallery app.
If I use Environment.getExternalStoragePublicDirectory , samsung devices return a path in internal memory (/storage/emulated/0/ ... )
If I use Context.getExternalFilesDirs , there are two results : the first one on internal storage, and the second one on SD Card. Ok, I can write inside and the photo is on the SDCard. But I can't see it in the Galery app :'(
I have tried to write directly on /storage/externalSdCard/DCIM/ but of course I can't since I'm running kitkat.
Ok, I can write inside and the photo is on the SDCard. But I can't see it in the Galery app
First, when you are done writing to the file, call flush(), then getFD().sync(), then close(), all on your FileOutputStream.
Then, use MediaScannerConnection and its scanFile() method to get the newly-written file indexed by the MediaStore.
void saveImage() {
File filename;
try {
String path = Environment.getExternalStorageDirectory().toString();
new File(path + "/folder/subfolder").mkdirs();
filename = new File(path + "/folder/subfolder/image.jpg");
FileOutputStream out = new FileOutputStream(filename);
bitMapImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
MediaStore.Images.Media.insertImage(getContentResolver(), filename.getAbsolutePath(), filename.getName(), filename.getName());
Toast.makeText(getApplicationContext(), "File is Saved in " + filename, 1000).show();
} catch (Exception e) {
e.printStackTrace();
}
}

Android delete all files starting with particular word

I have a problem and I hope you can help me out.
In my wallpaper app, I am saving an image to SDcard so the user can share it online via ActionBar share function.
Here is the code that does the saving:
Uri bmpUri = null;
try {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
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;
Now, I am trying to delete that image when the user leaves the activity (which means he already shared the image or decided not to share. Either way, its not needed anymore.)
I am trying to delete it with this code (Its on button click method atm, but will change that later)
case R.id.btn_delete:
// File folder = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
File folder = new File(Environment.getExternalStorageDirectory() + Environment.DIRECTORY_DOWNLOADS);
File[] filenamestemp = folder.listFiles();
for (int i = 0; i < filenamestemp.length; i++) {
if (filenamestemp[i].getAbsolutePath().toString().contains("share_image_"))
filenamestemp[i].delete();
}
break;
The way I see it, this should work...but it doesn't.
I tried all sorts of things, but nothings works. No errors,nothing..but image is still present in Downloads directory.
Any help?
I think you are getting the wrong directory for the written files
You are reading the files by
File folder = new File(Environment.getExternalStorageDirectory() + Environment.DIRECTORY_DOWNLOADS);
instead of:
File folder = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
These directories are not the same.
Hope it helps, if you need anything else tell me ;)

Can't see the bitmap I saved to new directory

I was looking for a way to save a bitmap to a folder (with my app's name), in such a way the default gallery will recognize it.
So I managed to save the image, but I can't see it either from the gallery or my PC (using explorer)
this is my code:
// Save bitmap to internal memory
private void savePhoto(Bitmap bmp){
String appName = "myApp";
String file_path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator + appName;
File dir = new File(file_path);
if(!dir.exists())
dir.mkdirs();
// Image file
File file = new File(dir, "IMG" + "_" + System.currentTimeMillis() + ".jpg");
FileOutputStream out = null;
try
{
out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
out = null;
} catch (Exception e)
{
e.printStackTrace();
}
}
I can see it using some File Manager installed on my galaxy, but it doesn't help a lot.
I know I should inform the gallery using a media scanner (which is another bridge I need to cross) but may someone help me understand way I can't even find the file..should I change its visibility somehow?
and one more thing: I read at another question regarding the issue, that I should add metadata to the image/folder so that the gallery would show it. Is it necessary?
Many thanks!!!
Apparently it did have something to do with the media scanner, I added this line
MediaScannerConnection.scanFile(this, new String[] { file.getAbsolutePath() }, null, null);
and now it works.
from the documentation:
MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service. The media scanner service will read metadata from the file and add the file to the media content provider.
try to replace this getExternalStoragePublicDirectory(....)
with this one getExternalStorageDirectory();

Categories

Resources