Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("image/*");
Uri uri = Uri.parse(pathToImage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;
i was used above code for sharing image on social sites.when i posting image on facebook only text is posted and image is not coming.how can we get the image and pathtoimage is string variable i am getting sever image path and stored in string variable.but it is not working.please give any solutions for my above question.?
Try this
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"));
Refer more links
http://android-developers.blogspot.in/2012/02/share-with-intents.html
Android Share Via Dialog
Image post into facebook wall from android sdcard
This is the solution I came up with:
This function allows you to share to instagram, facebook or any other app if you know the package name. If the app is not installed it redirects to the Play Store. If you send "others" as parameters, you will display a list of apps that also support image sharing.
shareImageIntent(mActivity,"image/*", "/storage/emulated/0/Pictures/Instagram/IMG_20150120_095603.jpg", "Instagram Sharing test. #Josh","com.instagram.android"); // parameters - for facebook: "com.facebook.katana" , to display list of other apps you can share to: "others"
public void shareImageIntent(Activity a,String type, String mediaPath, String caption, String app){
Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(app);
if ((intent != null)||(app.equals("others"))){
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
if(!app.equals("others"))
share.setPackage(app);
// Set the MIME type
share.setType(type);
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI and the caption to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TEXT, caption);
// Broadcast the Intent.
//startActivity(Intent.createChooser(share, "Share to"));
a.startActivity(share);
}
else{
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//intent.setData(Uri.parse("market://details?id="+"com.instagram.android"));
intent.setData(Uri.parse("market://details?id="+app));
a.startActivity(intent);
}
}
You can not use Intent to post to Facebook. They have specifically blocked this. You have to either use their SDK or copy it to the clipboard and have the user paste it after the facebook interface opens. I am having this same problem.
Related
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);
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.
I'm having a problem with sharing content on android between apps, with Intent
I can successfully share plain text (on G+, Facebook and Facebook Messenger)
The code is the following:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT,"Your score is: "+Integer.toString(mScore));
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
But when it comes to images things don't work.
Intent is created and I can actually choose between the apps to share the image with, but when the selected app tries to start, it just crashes and doesn't open.
No app works (tested with 9Gag, G+, Facebook, Facebook Messenger, Photos..)
The code I'm using is this:
Intent shareIntent = new Intent();
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
The image is stored on
/data/data/com.test.carloalberto.myapp/files/score.png
which is the right path where i previously saved the image.
Why do other apps crash when trying to share an image?
Thanks for reply.
EDIT: I stored the image in the public picture directory, but apps like fb messenger trying to share it still crash!
This is the new code:
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String name = "score.jpg";
File file = new File(dir,name);
FileOutputStream ostream;
try {
if(!dir.exists()) {
Log.i("Share: onClick()","Creating directory "+dir);
dir.mkdirs();
}
Log.i("Share: onClick()","Final path: "+file.getAbsolutePath());
file.createNewFile();
ostream = new FileOutputStream(file);
Log.i("onClick(): Share action: ","Path: "+file.getAbsolutePath());
bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
ostream.close();
Uri uriToImage = Uri.parse(file.getAbsolutePath());
Log.i("onClick(): Share action, final uri ",uriToImage.toString());
Intent shareIntent = new Intent();
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent,getResources().getText(R.string.send_to)));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
Now the file path is: /mnt/sdcard/Pictures/score.jpg, which should be public and accessible by all other applications.
(Obviously I also set the permissions in the manifest file)
/data/* path is not available to apps (security model of the filesystem every *nix user should be aware of).
The fact you are able to access this folder on your rooted device does not mean the app could do the same.
You could either store your image in publicly available directory (such as Pictures folder of the device, etc), or try to construct URI for your asset using the scheme:
android.resource://[package]/[res type]/[res name]
Try this.
private void shareIntent(String type,String text){
File filePath = getFileStreamPath("shareimage.jpg");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(filePath)));
shareIntent.setType("image/*"); //"image/*" or "image/jpeg", etc
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
}
I have image in assets folder and need to share it with whatsapp application
I tried this code , it keeps give me sharing failed try again ! what's wrong ?!
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.setPackage("com.whatsapp");
// share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("file:///assets/epic/adv.png")));
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///assets/epic/adv.png"));
this.startActivity(Intent.createChooser(share, "share_via"));
This code for share image via whatsapp worked fine for me .
public void shareImageWhatsApp() {
Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.adv);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "temporary_file.jpg");
try {
f.createNewFile();
new FileOutputStream(f).write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse( Environment.getExternalStorageDirectory()+ File.separator+"temporary_file.jpg"));
if(isPackageInstalled("com.whatsapp",this)){
share.setPackage("com.whatsapp");
startActivity(Intent.createChooser(share, "Share Image"));
}else{
Toast.makeText(getApplicationContext(), "Please Install Whatsapp", Toast.LENGTH_LONG).show();
}
}
private boolean isPackageInstalled(String packagename, Context context) {
PackageManager pm = context.getPackageManager();
try {
pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
You have several problems.
First, file:///assets/ is not a valid Uri on any version of Android. Your own application can refer to its own assets via file:///android_asset/.
Second, only you can access your own assets via file:///android_asset/ -- you cannot pass such a Uri to third-party apps. Either copy the file from assets into internal storage and use FileProvider, or you can try my StreamProvider and try to share the data straight out of assets/.
Third, there is no guarantee that com.whatsapp exists on the device, or that com.whatsapp will support ACTION_SEND of file:/// Uri values with a MIME type of image/*, and so you may crash with an ActivityNotFoundException.
Fourth, the user might want to share this image via some other means than WhatsApp. Please allow the user to share where the user wants, by removing the setPackage() call from your Intent.
This worked for me
Bitmap imgBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.image);
String imgBitmapPath= MediaStore.Images.Media.insertImage(getContentResolver(),imgBitmap,"title",null);
Uri imgBitmapUri=Uri.parse(imgBitmapPath);
Intent shareIntent=new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
startActivity(Intent.createChooser(shareIntent,"share image using"));
How can i post an image and a text to instagram?
no problem if i must open the instagram app, or if i do via api or other.
the problem is that i can't find how to: they have the iphone hook only, and the api is incomprensible.
someone have did that, or have the know?
thanks.
Try this it is worked for me. I will use it for share my product details. i will get details of product like name,image,description from the server and display in app and then share it to instagram.
Get image url from server.
URL url = ConvertToUrl(imgURL);
Bitmap imagebitmap = null;
try {
imagebitmap = BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Post image and text on instagram.
// post on instagram
Intent shareIntent = new Intent(
android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM,
getImageUri(HomeActivity.this, result));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, sharename);
shareIntent.putExtra(
Intent.EXTRA_TEXT,
"Check this out, what do you think?"
+ System.getProperty("line.separator")
+ sharedescription);
shareIntent.setPackage("com.instagram.android");
startActivity(shareIntent);
Convert Uri String to URL
private URL ConvertToUrl(String urlStr) {
try {
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(),
url.getHost(), url.getPort(), url.getPath(),
url.getQuery(), url.getRef());
url = uri.toURL();
return url;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Convert Bitmap to Uri
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(
inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
I doubt the Instagram developers will ever release iPhone like hooks for android as Intents already serve this purpose.
If you want to share an image from your app use an intent like this:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/jpeg");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Path/To/Image.jpg"));
startActivity(Intent.createChooser(i, "Share Image"));
Note that this will show a dialoge allowing the user to pick which photo sharing app to launch.
This works for me.... but it assumes the package name of com.instagram.android, which may change without notice I suppose.
private void shareToInstagram(Uri imageUri){
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, imageUri);
i.setPackage("com.instagram.android");
activity.startActivity(i);
}
You could always check to see if the com.instagram.android package is installed using getPackageManager() ... and if not, then don't set the package name in the intent and let the app chooser help the user pick the app to share the image with.