Clear share intent data while whatsapp sharing in android - android

I have a Relative layout and Textview on it. Then I convert that relative layout to bitmap image and save in directory.
The problem is when I am sharing the image, in most of app, I found correct image but in case of whatsapp when I am sharing through whatsapp the preview of sharing image is showing me old one until I close the app. Why it's not getting update and when I share the image on whatsapp, preview is old one and the image goes right one. This works fine in FB, Gmail etc...
This is the below code for converting and saving converted bitmap image in file system (directory)
This is for creating Direcory:
File dir = null;
String directorySubPath = "Android/data/com.domainname.appname/sharedResource/";
String imageNameForSave = "/qqq.png";
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
Toast.makeText(this, "Your device doesn't have external storage.", Toast.LENGTH_SHORT).show();
return;
}
else
{
Log.d("SD", "YES", null);
dir = new File(Environment.getExternalStorageDirectory()+File.separator+directorySubPath);
if (!dir.exists()){
dir.mkdirs();
}
else {
Log.d("Q-Design:", "Already created", null);
}
}
This is for code convert layout to bitmap image & save.
rLayout.setDrawingCacheEnabled(true);
Bitmap bmp = Bitmap.createBitmap(rLayout.getDrawingCache());
FileOutputStream out = new FileOutputStream(dir+imageNameForSave);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
rLayout.setDrawingCacheEnabled(false);
This is for SHARING that image via Intent:
Uri uri = Uri.parse("file:///" + dir + imageNameForSave);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test Mail");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Data Shared with you...");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share via:"));

Use a dynamic name in String imageNameForSave
Ex. String imageNameForSave = "/qqq" + mydate + ext;

Related

Send screenshot by email Android 8.0

I've asked a similar question before but I figured it out myself. But I now have a new problem, my device updated to android 8.0 and now email intent is not working. I don't want to downgrade my device if possible.
private void sendScreen() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".png";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.PNG, quality, outputStream);
outputStream.flush();
outputStream.close();
File filelocation = new File(MediaStore.Images.Media.DATA + mPath);
Uri myUri = Uri.parse("file://" + filelocation);
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"Enter your email address"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Journey : " + now );
startActivity(Intent.createChooser(emailIntent , "Select your preferred email app.."));
} catch (Throwable e) {
// Several error may come out with file handling or DOM
e.printStackTrace();
}
}
This is my code which worked perfectly on android 7.0. The code takes a screenshot, time stamps it, saves it to the local storage and then attached it to an email app of the users choice. Anyone have a solution? Thanks
This is my code which worked perfectly on android 7.0
It should have crashed with a FileUriExposedException.
now email intent is not working
You did not explain what "is not working" means. I am going to guess that you are crashing with a FileUriExposedException. A file Uri — whether via Uri.fromFile() or your Uri.parse("file://"+...) approach — is not usable on Android 7.0+ by default. Switch to using FileProvider and getUriForFile().

WhatsApp shared image just black

if (Utils.isPackageInstalled(getContext(), "com.whatsapp")) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
Bitmap bitmap = Utils.screenShotBitmap(getActivity());
Utils.saveImage(getActivity(), bitmap, bet.getID() + ".jpeg");
File file = new File(getActivity().getFilesDir(), bet.getID() + ".jpeg");
if (file.exists()) {
Log.i("share", "file exists");
Log.i("share", Uri.fromFile(file).toString());
}
sendIntent.putExtra(Intent.EXTRA_TEXT, "Share text");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
sendIntent.setType("image/*");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
}
// ...
public static Bitmap screenShotBitmap(Activity activity) {
return Falcon.takeScreenshotBitmap(activity);
}
public static void saveImage(Context context, Bitmap b, String name){
FileOutputStream out;
try {
out = context.openFileOutput(name, Context.MODE_PRIVATE);
b.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Log-Output:
03-25 17:13:24.328 26417-26417/x.x.x I/share: file exists
03-25 17:13:24.328 26417-26417/x.x.x I/share: file:///data/data/x.x.x/files/4b00abc2-7aae-4234-945b-59905306ad4a.jpeg
Result:
Bitmap bitmap = Utils.screenShotBitmap(getActivity()); seems to work fine and return a correct Bitmap, because I can share it without problems to facebook.
because I can share it without problems to facebook
Not with that code.
You are writing the file to internal storage. Neither Facebook nor WhatsApp has access to your portion of internal storage. Plus, using file: Uri values is being phased out.
The correct long-term answer is for you to use a ContentProvider to publish the bitmap, such as using FileProvider. In the short term, you can probably get away with writing to external storage, such as getExternalFilesDir().
Also note that ACTION_SEND recipients do not need to honor both EXTRA_TEXT and EXTRA_STREAM, but only one or the other.

Why email attachment is empty attached from external storage?

Developing Android email plugin for Unity. I have a screenshot in the files/ folder of the app, I want to attach to mail. As it turned out, I cannot attach from there directly. I implemented a FileProvider, but it turned out that it exist only above 4.0.
So I implemented the suggested workaround, to save it to external storage, then attach from there. Saving seems work, even reading seems work, but still, Gmail says "Can't attach empty file". Also When launching email intent, I have an error message, like:
E/HwEmailTag( 7327): AttachmentUtilities->inferMimeTypeForUri->Unable to determine MIME type for uri=/storage/emulated/0/com.eppz.plugins_screenshot.jpg
I tried application/image, image/jpg as intent.setType(), still the same, while Gmail says the file is empty.
Is this something with emulated external storage /storage/emulated/0/? The device has no SD card, but I've read that getExternalStorage() returns a shared / public place for files in such cases either.
It should work. Should I remove dots from filename? Hope not. Here's the corresponding code:
String saveImageAtPathToExternalStorage(String imagePath)
{
Log.i(TAG, "saveImageAtPathToExternalStorage(...)");
// Create bitmap.
File imageFile = new File(imagePath);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), new BitmapFactory.Options());
// Output.
String outputFileName = _unityPlayerActivity.getPackageName()+"_screenshot.jpg";
String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
File outputImageFile = new File(externalStorageDirectory, outputFileName);
String outputImagePath = outputImageFile.getAbsolutePath();
if (outputImageFile.exists()) outputImageFile.delete(); // Delete if existed
try
{
// Write JPG.
FileOutputStream outputStream = new FileOutputStream(outputImageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
Log.i(TAG, "Image written to `"+outputImagePath+"`");
}
catch (Exception e)
{ e.printStackTrace(); }
// Return with output path.
return outputImagePath;
}
public void openMailComposer(String to, String subject, String body, int isHTML, String attachmentImagePath)
{
Log.i(TAG, "openMailComposer(...)");
// Attachment image.
File attachmentImageFile = new File(attachmentImagePath);
if (attachmentImageFile.exists() == false)
{
Log.i(TAG, IMAGE_NOT_FOUND);
SendUnityMessage(OPEN_MAIL_COMPOSER_CALLBACK_METHOD_NAME, IMAGE_NOT_FOUND);
return;
}
// Save to external first.
String externalImagePath = saveImageAtPathToExternalStorage(attachmentImagePath);
final Uri externalImageUri = Uri.parse(externalImagePath);
Log.i(TAG, "externalImageUri `"+externalImageUri+"`");
// Intent.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, body);
if (isHTML == 1) intent.putExtra(Intent.EXTRA_HTML_TEXT, body);
// Attach.
intent.putExtra(Intent.EXTRA_STREAM, externalImageUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(Intent.createChooser(intent, "Send email"), OPEN_MAIL_COMPOSER_REQUEST_CODE);
}
I'm relatively new to Android development, and really want to believe, but having all this hassle compared to having a single line for this in iOS is quiet distressing.
I'm running this on a Huawei MediaPad (TT1 7.0), Android 4.4.2, and I want it to run about Android 2.3+ basically (why I refused using FileProvider earlier).

Problems on sharing bitmap images

I have used com.nostra13.universalimageloader to load image. Inside the standard ImageAdapter in a ViewPager, there is a share button to share the bitmap. The code is as follows:
Code:
#Override
public Object instantiateItem(ViewGroup view, int position)
{
....
#Override
public void onLoadingComplete(String imageUri, View view, final Bitmap loadedImage)
{
spinner.setVisibility(View.GONE);
btn_share = (ImageButton) imageLayout.findViewById(R.id.btn_share);
btn_share.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Shining Card";
File dir = new File(file_path);
if (!dir.exists()) dir.mkdirs();
File file = new File(dir, "to share");
FileOutputStream fOut;
try
{
fOut = new FileOutputStream(file);
loadedImage.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
}
catch (Exception e)
{
e.printStackTrace();
Constants.custom_toast(getActivity(), "Error when sharing", "");
}
Uri uri = Uri.fromFile(file);
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, "");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Share Cover Image"));
}
});
}
Question:
The problem lies on sharing the image.
Share via whatsapp
Sharing the first image via whatsapp, the image can be shared successfully to others. Continues with sharing second image it also works successfully.
However, if the sharing of the first image is cancelled in whatsapp and return to the app, and then choose another image to share in whatsapp, the preview in whatsapp will remain as the first image, though if ignore it and continue to share whatsapp will actually share the new image. (i.e. there is a preview problem) (using other app to share image perform the same, preview in whatsapp does not go wrong)
Share via gmail
Sharing the image as attachment via gmail, the image cannot be previewed nor opened.

when sending image from assets by email the mailer view shows 0kb size of image?

i am a new android developer and in my app i want to send an image via email which is placed in Assets folder and the mailer view shows 0 kb size of image attachment but the name of image is properly shown and here is my code is:
try {
Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);
picMessageIntent.setType("image/png");
String str = "image.png"
String imageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"file:///android_asset/Images/" + str;
File downloadedPic = new File(imageDirectoryPath);
picMessageIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(downloadedPic));
startActivity(Intent.createChooser(picMessageIntent, "Send your picture using:"));
} catch (Exception e) {
Toast.makeText(MyActivity.this, "No handler", Toast.LENGTH_LONG).show();
}
Pleas give me any suggestion...
Thanx in Advance...
There is some problem with MIME Type change the MIME type and try again.
picMessageIntent.setType("image/*");
or
picMessageIntent.setType("image/jpg");

Categories

Resources