Android. Sharing image error - android

I write the app that process image somehow and than I want to share it. Meaning to allow user to share it using standard share activity. When I do this I get error messages. When I choose Gmail it writes Can't open empty file. But the file is saved, I can open it with the Gallery or any other app. So I can not figure out what I do wrong.
Here is my sharing code:
public static void shareImage(Bitmap bmp, Context context) {
String pathBitmap = saveBitmap(bmp, context);
if (pathBitmap == null) {
Toast.makeText(context, context.getResources().getString(R.string.save_photo_failed), Toast.LENGTH_SHORT).show();
return;
}
Uri bitmapUri = Uri.parse(pathBitmap);
if (bitmapUri != null) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
context.startActivity(Intent.createChooser(shareIntent, "Share"));
}
}
Here is how I save the bitmap:
public static String saveBitmap(Bitmap bmp, Context context) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
String fileName = sdf.format(new Date(System.currentTimeMillis()));
File fNew = new File(MyApp.getInstance().getPhotosPath(), fileName + ".png");
FileOutputStream out = null;
try {
out = new FileOutputStream(fNew);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
out.close();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
Toast.makeText(context, context.getResources().getString(R.string.photos_saved), Toast.LENGTH_SHORT).show();
return fNew.getAbsolutePath();
}
UPDATE
Here are functions that returns the path. createFolder() is called from apps onCreate()
private void createFolder() {
_pathPhotos = "";
try {
File folder = new File(Environment.getExternalStorageDirectory(), "WonderApp");
folder.mkdir();
_pathPhotos = folder.getAbsolutePath();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public String getPhotosPath() {
return _pathPhotos;
}
What's wrong with the code?

Finally I resolved the problem. The problem actually was described here. As GuiFGDeo kindly wrote, the problem is URI that should be content URI instead of file URI. Here you can find how Google propose to solve the problem. I found a bit shorter way.
When you insert image into media store you receive exactly what you need - content URI. And voilà!
public static void shareImage(Bitmap bmp, Context context) {
String sImageUrl = MediaStore.Images.Media.insertImage(context.getContentResolver(), bmp, "title" , "description");
Uri savedImageURI = Uri.parse(sImageUrl);
if (savedImageURI != null) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
shareIntent.putExtra(Intent.EXTRA_STREAM, savedImageURI);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(shareIntent, "Share"));
}
}

Related

How to Share both image and text through whatsapp android programatically

I need to share both an image and some text via WhatsApp, using an Intent on Android.
Uri imageUri = Uri.parse(Filepath);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.whatsapp");
shareIntent.putExtra(Intent.EXTRA_TEXT, "My sample image text");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imageUrl));
shareIntent.setType("image/png");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Kindly install whatsapp first");
}
I’m using the code above, but it throws a 'File format not supported' error while sharing.
You can try like this
public static void shareOnWhatsapp(Context context, String str) {
Uri parse = Uri.parse(str);
Intent intent = new Intent();
intent.setAction("android.intent.action.SEND");
intent.setPackage("com.whatsapp");
String stringBuilder = context.getResources().getString(R.string.play_more_app) +
context.getPackageName();
intent.putExtra("android.intent.extra.TEXT", stringBuilder);
intent.putExtra("android.intent.extra.STREAM", parse);
intent.setType("image/*");
intent.addFlags(1);
try {
context.startActivity(intent);
} catch (Exception unused) {
setToast(context, context.getResources().getString(R.string.whatsapp_not_installed));
}
}
I used this function to share images it's working I hope it's work you too.
Edit
Add this dependency for converting URL to bitmap
implementation 'com.github.bumptech.glide:glide:4.14.2'
Use this function to get Image URI to share
private Uri getImageURI(Bitmap image) {
File imagesFolder = new File(getCacheDir(), "images");
Uri uri = null;
try {
imagesFolder.mkdirs();
File file = new File(imagesFolder, "shared_image.png");
FileOutputStream stream = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.PNG, 90, stream);
stream.flush();
stream.close();
uri = FileProvider.getUriForFile(this, getPackageName() + ".provider", file);
} catch (IOException e) {
Log.d("TAG", "IOException while trying to write file for sharing: " + e.getMessage());
}
return uri;
}
Use this function for share Image on WhatsApp
public static void shareOnWhatsapp(Context context, Uri uri) {
Intent intent = new Intent();
intent.setAction("android.intent.action.SEND");
intent.setPackage("com.whatsapp");
String stringBuilder = "Your String Or Message" +
context.getPackageName();
intent.putExtra("android.intent.extra.TEXT", stringBuilder);
intent.putExtra("android.intent.extra.STREAM", uri);
intent.setType("image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
context.startActivity(intent);
} catch (Exception unused) {
Toast.makeText(context, "whatsapp not installed", Toast.LENGTH_SHORT).show();
}
}
Uses like this
findViewById(R.id.btnShare).setOnClickListener(v -> {
Glide.with(this).asBitmap().load( /*Your URL*/ "https://drinkprime.in/images/smart_water_purifier.jpg").into(new CustomTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
Bitmap bitmap = resource;
shareOnWhatsapp(MainActivity.this, getImageURI(bitmap));
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});
});
It's Working now completely

Sharing an image in Android app using intent sends it as text file, not as a photo

I am trying to take a screenshot programatically and then share it to another app, using Intent. The problem that I face is that the photo is sent as a text file.
The button listener looks like this:
shareButton = rootView.findViewById(R.id.shareButton);
shareButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bitmap screenshot = Screenshot.takeScreenshot(view.getRootView());
String filename = "eduMediaPlayerScreenshot";
String sharePath = Screenshot.storeScreenshot(screenshot, filename);
Intent intent = Screenshot.shareScreenshot(sharePath, view.getRootView());
startActivity(intent);
}
});
And the Screenshot class like this:
public class Screenshot {
public static Bitmap takeScreenshot(View view) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap screenshot = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return screenshot;
}
public static String storeScreenshot(Bitmap screenshot, String filename) {
String path = Environment.getExternalStorageDirectory().toString() + "/" + filename;
OutputStream out = null;
File imageFile = new File(path);
try {
out = new FileOutputStream(imageFile);
screenshot.compress(Bitmap.CompressFormat.JPEG, 99, out);
out.flush();
} catch (FileNotFoundException e) {
Log.i("Exception:", "File not found.");
} catch (IOException e) {
Log.i("Exception:", "Cannot write to output file.");
} finally {
try {
if (out != null) {
out.close();
return imageFile.toString();
}
} catch (Exception e) {
Log.i("Exception:", "No output file to close.");
}
}
return null;
}
public static Intent shareScreenshot(String sharePath, View view) {
File file = new File(sharePath);
Uri uri = FileProvider.getUriForFile(view.getContext(),
BuildConfig.APPLICATION_ID + ".provider", file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
return intent;
}
}
The shared photo looks like this:
not-the-best-photo
As Android will not recognize by default an image without extension, you should add .jpg to the end of the filename in the intent above. Without it, there should be a MIME type specified. More details on that here.
This is the code you might want to use.
shareButton = rootView.findViewById(R.id.shareButton);
shareButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bitmap screenshot = Screenshot.takeScreenshot(view.getRootView());
String filename = "eduMediaPlayerScreenshot.jpg";
String sharePath = Screenshot.storeScreenshot(screenshot, filename);
Intent intent = Screenshot.shareScreenshot(sharePath, view.getRootView());
startActivity(intent);
}
});

Using ShareActionProvider on a CardView in a Recyclerview

I am trying to provide a share feature on the cards displayed in my android application. I have learn't about the ShareactionProvider. This actionProvider is suppose to be present on every cardview. I have placed the code for the ShareActionprovider on my onBindViewHolder in the Adapter. When the app starts, all the cards execute their share at the same time. I will like to call this share only when the user requests.
#Override
public void onBindViewHolder(final ViewHolder viewHolder, final Cursor cursor) {
final NewsFacade facade = NewsFacade.fromCursor(cursor);
MenuItem shareMenuItem = viewHolder.toolbar.getMenu().findItem(R.id.share);
ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareMenuItem);
shareActionProvider.setShareIntent(viewHolder.sendShareIntent(facade, context));
shareActionProvider.setShareHistoryFileName("custom_share_history.xml");
And Here are the Helper Methods to aid in the sharing:
public Intent sendShareIntent(NewsFacade facade, Context context)
{
saveImageToCache(facade, context);
return shareImage(context);
}
private void saveImageToCache(NewsFacade facade, Context context)
{
try {
Bitmap bitmap = facade.getThumb();
File cachePath = new File(context.getCacheDir(), "images");
cachePath.mkdirs();
FileOutputStream stream = new FileOutputStream(cachePath + "/image.png"); // overwrites this image every time
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private Intent shareImage(Context context)
{
File imagePath = new File(context.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(context, "com.example.clinton.companion.fileprovider", newFile);
if (contentUri != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setDataAndType(contentUri,context.getContentResolver().getType(contentUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
context.startActivity(Intent.createChooser(shareIntent, "Choose an app"));
return shareIntent;
}
return null;
}
I know, I am calling the ActionProvider in the wrong place. But where else, should I call this, I need an instance of the object that cardview holds, so I can get the image in the cardview to share.
Please any help will be apreciated.
I fixed it.
I was calling
context.startActivity(Intent.createChooser(shareIntent, "Choose an app"));
which automatically executes the sharing.

How to send drawable Images from android app as MMS or on any social media app?

I'm not able to share my image from the app for messaging as an attachment
I have a list of int array of drawable images
final int [] imagelistId={
R.drawable.birthday1,
R.drawable.birthday2,
R.drawable.birthday3,
R.drawable.birthday4,
R.drawable.birthday5,
R.drawable.birthday6,
};
After that i have this code for sharing an image as an attachment
smsBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
// add the message at the sms_body extra field
smsIntent.setData(Uri.parse("mmsto:"));
smsIntent.setType("image/*");
smsIntent.putExtra("sms_body", "Image");
smsIntent.putExtra("subject", "Image Message");
smsIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("android.resource://com.example.finalgreetings/" + imagelistId[pos]) );
try{
mcontext.startActivity(smsIntent);
} catch (Exception ex) {
Toast.makeText(mcontext, "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
});
I have seen many stack overflow questions but none answered my problem.
I have also read about converting drawable to bitmap and save to internal or external storage then share it but dont know how to do it.
Kindly suggest me best and easy solution. Thanx in advance.
Try this..!!!
smsBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yourimage);//put here your image id
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"));
}
});

How can I post on Twitter with Intent Action_send?

I have been struggling to send text from my app to Twitter.
The code below works to bring up a list of apps such as Bluetooth, Gmail, Facebook and Twitter, but when I select Twitter it doesn't prefill the text as I would have expected.
I know that there are issues around doing this with Facebook, but I must be doing something wrong for it to not be working with Twitter.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Example Text");
startActivity(Intent.createChooser(intent, "Share Text"));
I'm using this snippet on my code:
private void shareTwitter(String message) {
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "This is a Test.");
tweetIntent.setType("text/plain");
PackageManager packManager = getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for (ResolveInfo resolveInfo : resolvedInfoList) {
if (resolveInfo.activityInfo.packageName.startsWith("com.twitter.android")) {
tweetIntent.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
resolved = true;
break;
}
}
if (resolved) {
startActivity(tweetIntent);
} else {
Intent i = new Intent();
i.putExtra(Intent.EXTRA_TEXT, message);
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://twitter.com/intent/tweet?text=" + urlEncode(message)));
startActivity(i);
Toast.makeText(this, "Twitter app isn't found", Toast.LENGTH_LONG).show();
}
}
private String urlEncode(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.wtf(TAG, "UTF-8 should always be supported", e);
return "";
}
}
Hope it helps.
you can simply open the URL with the text and Twitter App will do it. ;)
String url = "http://www.twitter.com/intent/tweet?url=YOURURL&text=YOURTEXT";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
and it will also open the browser to login at the tweet if twitter app is not found.
Try this, I used it and worked great
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/intent/tweet?text=...."));
startActivity(browserIntent);
First you have to check if the twitter app installed on the device or not then share the text on twitter:
try
{
// Check if the Twitter app is installed on the phone.
getActivity().getPackageManager().getPackageInfo("com.twitter.android", 0);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setClassName("com.twitter.android", "com.twitter.android.composer.ComposerActivity");
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Your text");
startActivity(intent);
}
catch (Exception e)
{
Toast.makeText(getActivity(),"Twitter is not installed on this device",Toast.LENGTH_LONG).show();
}
For sharing text and image on Twitter, more controlled version of code is below, you can add more methods for sharing with WhatsApp, Facebook ... This is for official App and does not open browser if app not exists.
public class IntentShareHelper {
public static void shareOnTwitter(AppCompatActivity appCompatActivity, String textBody, Uri fileUri) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.setPackage("com.twitter.android");
intent.putExtra(Intent.EXTRA_TEXT,!TextUtils.isEmpty(textBody) ? textBody : "");
if (fileUri != null) {
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/*");
}
try {
appCompatActivity.startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
showWarningDialog(appCompatActivity, appCompatActivity.getString(R.string.error_activity_not_found));
}
}
public static void shareOnWhatsapp(AppCompatActivity appCompatActivity, String textBody, Uri fileUri){...}
private static void showWarningDialog(Context context, String message) {
new AlertDialog.Builder(context)
.setMessage(message)
.setNegativeButton(context.getString(R.string.close), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setCancelable(true)
.create().show();
}
}
For getting Uri from File, use below class:
public class UtilityFile {
public static #Nullable Uri getUriFromFile(Context context, #Nullable File file) {
if (file == null)
return null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
return FileProvider.getUriForFile(context, "com.my.package.fileprovider", file);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} else {
return Uri.fromFile(file);
}
}
// Returns the URI path to the Bitmap displayed in specified ImageView
public static Uri getLocalBitmapUri(Context context, ImageView imageView) {
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable) {
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
// Use methods on Context to access package-specific directories on external storage.
// This way, you don't need to request external read/write permission.
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = getUriFromFile(context, file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
}
For writing FileProvider, use this link: https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents

Categories

Resources