Share image with Android intent - android

I'm trying to share an image trough a share intent like this:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, application.getString(R.string.app_name));
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,application.getString(R.string.app_share_message));
File image = new File(Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share).toString());
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));
shareMe(sharingIntent);
The share intent fires correctly and I choose Gmail, everything runs as expected until I press send. I receive a notification "Unable to show attach", and the e-mail is sent without it...
Why?
Thanks for your time.

First, there is no guarantee that any given other app will be able to support an android:resource// Uri. You will have greater compatibility sharing a file on external storage or using a ContentProvider.
That being said, replace:
File image = new File(Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share).toString());
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));
with:
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share);
An android:resource:// is not a File, and probably you are messing up your Uri by converting to a File and then back to a Uri.

BitmapDrawable bitmapDrawable = (BitmapDrawable)ImageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
// Save this bitmap to a file.
File cache = getApplicationContext().getExternalCacheDir();
File sharefile = new File(cache, "toshare.png");
Log.d("share file type is", sharefile.getAbsolutePath());
try {
FileOutputStream out = new FileOutputStream(sharefile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (IOException e) {
Log.e("ERROR", String.valueOf(e.getMessage()));
}
// Now send it out to share
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + sharefile));
startActivity(Intent.createChooser(share,
"Share Image"));

In my case I used:
Uri imageUri = Uri.parse("android.resource://com.examle.tarea/" + R.drawable.tienda_musica);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(intent, getString(R.string.action_share)));

//open a share intent chooser (Will show installed app from which i can share //images)
private void shareImage(String imagePath, String quoteByPerson, String quoteToShare)
{
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
// String imagePath = Environment.getExternalStorageDirectory() +
// "/myImage.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.putExtra(Intent.EXTRA_TEXT, "Quote of the day-:" + "\n" + quoteToShare + "\n" + quoteByPerson);
share.putExtra(android.content.Intent.EXTRA_TITLE, "Quote of the day-:");
if (imagePath.contains("android.resource://"))
{
Uri imageUri = Uri.parse(imagePath);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
} else
{
share.putExtra(Intent.EXTRA_STREAM, uri);
}
startActivity(Intent.createChooser(share, "Share inspiration via..."));
}

Related

how to share text+video in single intent in android?

Below this code i try to share text with video but it only share video,text not share with video.
please help if any one have solution of my issue.
String path="android.resource://" + "com.avani.videoviewdemo" + "/" + R.raw.ae_kaash_kahi;
//Intent a = getIntent();
file=new File(path);
str=arrayList.get(position).getVideoName();
Log.e("video_name", ""+arrayList.get(position).getVideoName()); Log.e("video_name",""+ str);
id = context.getResources().getIdentifier(str, "raw", context.getPackageName()); Uri uri = Uri.parse("android.resource://" + context.getPackageName() + "/" + id);
DownloadFile();
Intent sharingIntent = new Intent(Intent.ACTION_SEND); sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri screenshotUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file); sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri); sharingIntent.putExtra(Intent.EXTRA_TEXT, "From FullScreenVideoStatus"); sharingIntent.setType("video/*"); context.startActivity(Intent.createChooser(sharingIntent, "Share video using"));
For set caption in video/image in share intent, just set text in:
sharingIntent.putExtra(Intent.EXTRA_TITLE, "Your text caption");
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("video/mp4"); //If it is a 3gp video use ("video/3gp")
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/raw/ae_kaash_kahi";
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Video Title");
startActivity(Intent.createChooser(sharingIntent, "Share Video!"));
intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "TEMPORARY"); // for text share
intent.putExtra(Intent.EXTRA_STREAM, uri); // for media share
intent.setType("image/*"); // this line is use to filter which app support specified media formate to share
startActivity(intent);
you can use video/* for video file here i have used image/* for image share

How to properly create an Intent to refer like Tez?

In my app i have to add an intent to share my app. I looked through Tez, which shares the app icon along with a text which contains a hyperlink. How to achieve this?
you can try this one..
Uri uri = Uri.fromFile(imageFile);
Intent intent1 = new Intent();
intent1.setAction(Intent.ACTION_SEND);
intent1.setType("image/*");
intent1.putExtra(android.content.Intent.EXTRA_SUBJECT, "App Name");
intent1.putExtra(Intent.EXTRA_TEXT, "Download the app from google play store now - "+ APP_STORE_URL);
intent1.putExtra(Intent.EXTRA_STREAM, uri);
try {
startActivity(Intent.createChooser(intent1, "Share"));
} catch (ActivityNotFoundException e) {
Toast.makeText(getContext(), "please try again", Toast.LENGTH_SHORT).show();
}
this will works : put image file and text box in share intent
private void prepareShareIntent(Bitmap bmp) {
Uri bmpUri = getLocalBitmapUri(bmp); // see previous remote images section
// Construct share intent as described above based on bitmap
Intent shareIntent = new Intent();
shareIntent = new Intent();
shareIntent.setPackage("com.whatsapp");
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.share_app) );
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share Opportunity"));
}
private Uri getLocalBitmapUri(Bitmap bmp) {
Uri bmpUri = null;
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
bmpUri = Uri.fromFile(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bmpUri;
}
It look like you want to create an referrer links for which try using this firebase service.Once you have got your referrer URL ready.Create an intent as follow to share it.
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subjectText);
shareIntent.putExtra(Intent.EXTRA_HTML_TEXT, "Hey!Checkout this app "+ APP_STORE_URL);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(shareIntent, "Invite Friends"));
**Note:**If you are using dynamic links you can add your app icon in the social meta tags param
Try this
Uri imageUri = Uri.parse("android.resource://" + getPackageName() + "/drawable/" + "ic_launcher");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello" + REFERRAL_URL);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));`
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
/**This is the image to share**/
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "title");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
} catch (Exception e) {
System.err.println(e.toString());
}
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TEXT, "YOUR_BODY_TEXT_HERE");
startActivity(Intent.createChooser(share, "Share Image"));
I've tested the above code, it works as per your requirement.
PS: You need to have WRITE_EXTERNAL_STORAGE permission. Be sure to include it and handle it according to SDK's.
This code will share both the files together
ArrayList<Uri> myFilesUriList = new ArrayList<>();
myFilesUriList.add(); // add your image path as uri
myFilesUriList.add(); // add your text file path as uri
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/*");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, myFilesUriList);
startActivity(intent);
This code will share both the files Separately
First share the file then on Activity Result, share text Separately
ArrayList<Uri> uriArrayList = new ArrayList<>();
uriArrayList.add(); // add your image path as uri
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/*");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriArrayList);
startActivityForResult(intent, 156);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 156) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/*");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, ""); //"Subject here"
sharingIntent.putExtra(Intent.EXTRA_TEXT, "shareBody ");
sharingIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(Intent.createChooser(sharingIntent, "Share text via.."));
}
}
It's URL Meta Data. These meta data is returned as response from server.
<meta property="og:site_name" content="San Roque 2014 Pollos">
<meta property="og:title" content="San Roque 2014 Pollos" />
<meta property="og:description" content="Programa de fiestas" />
<meta property="og:image" itemprop="image" content="http://pollosweb.wesped.es/programa_pollos/play.png">
<meta property="og:type" content="website" />
<meta property="og:updated_time" content="1440432930" />
Showing Thumbnail for link in WhatsApp || og:image meta-tag doesn't work
Step 1 - Read the image which you want to share
Step 2 - Store that image in your external storage
Step 3 - share via intent
// Extract Bitmap from ImageView drawable
Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.launcher); // your image
if (drawable instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
// Store image to default external storage directory
Uri bitmapUri = null;
try {
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();
bitmapUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
if (bitmapUri != null) {
Intent shareIntent = new Intent();
shareIntent.putExtra(Intent.EXTRA_TEXT, "I am inviting you to join our app. A simple and secure app developed by us. https://www.google.co.in/");
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share my app"));
}
}
Note - Add WRITE_EXTERNAL_STORAGE permission in your manifest. Ask runtime permission on Android 6.0 and higher.
You might be looking for deep linking to your app
https://developer.android.com/training/app-links/index.html
https://developer.android.com/training/app-links/deep-linking.html
Or if you want links to your app across platforms you can check out Firebase dynamic links
https://firebase.google.com/docs/dynamic-links/
Copy this code into your toolbar/menu
try {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Your Subject");
String text = "\nYour description";
text = text + "https://play.google.com/store/apps/details?id=apppackagename \n\n";
i.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(i, "Choose "));
}
catch(Exception e) {
//e.toString();
}
Try this code:
int applicationNameId = context.getApplicationInfo().labelRes;
final String appPackageName = context.getPackageName();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, activity.getString(applicationNameId));
String text = "Install this cool application: ";
String link = "https://play.google.com/store/apps/details?id=" + appPackageName;
i.putExtra(Intent.EXTRA_TEXT, text + " " + link);
startActivity(Intent.createChooser(i, "Share link:"));
if you want to share your other apps from your dev. account you can do something like this
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://play.google.com/store/apps/developer?
id=Your_Publisher_Name"));
startActivity(intent);

Sharing Image using intent in watsApp is not working

I am sharing two URI image resource which from mipmap and ACTION_GET_CONTENT used URI.
public void shareUsingIntent() {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, getUri());
startActivity(Intent.createChooser(i, "Share Image"));
}
public Uri getUri() {
if (selectedImageUri != null) {
return selectedImageUri;
} else {
return Uri.parse("android.resource://" + getPackageName() + "/" + R.mipmap.ic_launcher);
}
}
It was worked in ACTION_GET_CONTENT used URI but mipmap resource was not working in some application like Facebook and watsapp. I read from some stack answer that Image must be add in Extenrnal storage. Its not working for this URI.
Uri.parse("android.resource://" + getPackageName() + "/" + R.mipmap.ic_launcher
in what's app and Facebook and why it was working in other app like default messing app, Twitter etc.?
Try this function:
// if targetSDK >= 23, please check for runtime permission: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Also add the same permission to your Manifest file.
private void shareViaWhatsApp() {
Uri imageUri = null;
try {
imageUri = Uri.parse(MediaStore.Images.Media.insertImage(this.getContentResolver(),
BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher), null, null)); //You may need to check for permission for this.
} catch (NullPointerException e) {
}
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name));
emailIntent.putExtra(Intent.EXTRA_TEXT, "Text to share");
emailIntent.putExtra(Intent.EXTRA_STREAM, imageUri); //............Pass Image URI here.........
emailIntent.setType("image/*");
emailIntent.setPackage("com.whatsapp");
startActivity(Intent.createChooser(emailIntent, "Share..."));
}
You can try this:
boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
if (isWhatsappInstalled) {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,"http://play.google.com/store/apps/details?id=" + getPackageName());
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), getBitMap(), "I am Happy", "Share happy !")));
sendIntent.setType("image/png");
sendIntent.setPackage("com.whatsapp");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(sendIntent);
} else {
Toast.makeText(getApplicationContext(), "WhatsApp not Installed", Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse("market://details?id=com.whatsapp");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
startActivity(goToMarket);
}
where getBitMap() is function in which get the image bitmap which you want to share.
Use Below Code for every app in you can share image, like whats app, hike or mail and many other
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/png");
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.mipmap.ic_launcher);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, This is test Sharing");
startActivity(Intent.createChooser(shareIntent, "Send your image"));

Send Image as email attachement in android

I am getting an error that i cannot attach empty file in gmail.I am trying to build simple app in which when i click on the button it show choices by which i can send image,But the below code is not working.
Please help i am novice in android.
code:
if(view.getId()==R.id.SendImage)
{
Uri imageUri = Uri.parse("android:resource://com.example.jaspreet.intentstest.drawable/"+R.drawable.image);
intent=new Intent(android.content.Intent.ACTION_SEND);
intent.setType("application/image");
intent.putExtra(Intent.EXTRA_STREAM,imageUri);
intent.putExtra(Intent.EXTRA_TEXT,"Hey i have attached this image");
chooser=Intent.createChooser(intent,"Send Image");
startActivity(chooser);
}
Try using this:
shareIntent.setType("image/png");
By using this the intent know that it will send .png file.
On this link you can find a list of all media type/subtypes
Try this
Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
b, "Title", null);
Uri imageUri = Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Select"));
Try this snippet
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share);
startActivity(Intent.createChooser(share, "Select"));
An android:resource:// is not a File, and probably you are messing up your Uri by converting to a File and then back to a Uri.

Not able to share file with whitespace in name in android

I am not able to share audio file in whatsapp if there is any whitespace in the filename. But it works when sharing using email client. For filenames without spaces also it works fine. Below is the code which I am using
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
File F = new File(filePath);
F.setReadable(true, false);
Uri fileURI = Uri.fromFile(F);
Log.e("Share", "Share file url is " + fileURI);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Shared file");
sharingIntent.setType("*/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, fileURI);
I tried doing filePath.replace(" ", "\ "), not working.
What changes should be done to share the file?
This works when you trying to share with WhatsApp an audio file with whitespace:
String filePath = "file:///sdcard/Download/example attachment.mp3";
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath));
shareIntent.setType("audio/*");
startActivity(Intent.createChooser(shareIntent, "Share audio file"));
I was able to share audio using same code as I have posted with just a minor change in the type. Below code works for both email as well as whatsapp sharing:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
File F = new File(filePath);
F.setReadable(true, false);
Uri fileURI = Uri.fromFile(F);
Log.e("Share", "Share file url is " + fileURI);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Shared file");
sharingIntent.setType("audio/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, fileURI);

Categories

Resources