Sharing Image to Instagram Story - android

Using sharing intent i tried to upload image to Instagram using below code
public static boolean shareBitmap(Context context, Bitmap bitmap, String textToShare, String packageNameOfApp,
String intentMessage) {
try {
File file = new File(context.getCacheDir(), "Share.jpg");
saveBitmapAtLocation(bitmap, Bitmap.CompressFormat.JPEG, 100, file);
Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, contentUri);
String textToAppend = null;
textToAppend = context.getString(R.string.shortenUrl);
if (textToShare != null && !textToShare.isEmpty())
intent.putExtra(Intent.EXTRA_TEXT, textToShare + " " + textToAppend);
else
intent.putExtra(Intent.EXTRA_TEXT, textToAppend);
intent.setType("image/*");
List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
context.grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
if (packageNameOfApp != null) {
if (appInstalledOrNot((Activity) context, packageNameOfApp))
intent.setPackage(packageNameOfApp);
else
return false;
}
context.startActivity(Intent.createChooser(intent, intentMessage));
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
When i try to post directly to the Instagram story it gives a black screen with a Toast message Couldn't refresh feed

Set Instagram-app-package to intent-package will do your work.
Uri uri = Uri.fromFile(new File(YOUR_FILE_PATH))
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setPackage("com.instagram.android"); //Instagram App package
startActivity(Intent.createChooser(shareIntent, "Share.."));
Hopes it help you.

After long time research finally i found ans.
private void shareStory(){
Uri backgroundAssetUri = FileProvider.getUriForFile(this, "your_package_name.provider", imgFile);
Intent storiesIntent = new Intent("com.instagram.share.ADD_TO_STORY");
storiesIntent.setDataAndType(backgroundAssetUri, "image/*");
storiesIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
storiesIntent.setPackage("com.instagram.android");
this.grantUriPermission(
"com.instagram.android", backgroundAssetUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
this.startActivity(storiesIntent);
}

I had the same problem, and I found an alternative solution in my own question:
Xamarin Forms can't share image to Instagram stories
The only thing is that I'm developing with xamarin forms, not with native android, but I hope my answer helps 😅

Related

Unsupported attachment

private void sendPdf(String file) {
// Uri uri = Uri.fromFile(new File(file));
Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", new File(String.valueOf(file)));
intent = new Intent(Intent.ACTION_SEND);
// intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION & Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// intent.putExtra(Intent.EXTRA_SUBJECT, "Transaction history");
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_STREAM, uri); // invitation body
Intent chooser = Intent.createChooser(intent, "Share with: ");
List<ResolveInfo> resInfoList = getActivity().getPackageManager().queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
getActivity().grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
startActivity(chooser);
}
I trying to share pdf using WhatsApp/Telegram but when I try to share the file, but it say unsupported attachment(Telegram). Can anyone help me?
EDIT: I got this issue only for Android 10. Anyone facing same problem?
I use this piece of code for my own application.
Intent mIntent = new Intent(Intent.ACTION_SEND);
File mFile = new File(mPath);
if(mFile.exists()) {
mIntent.setType("application/pdf");
mIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + mPath));
mIntent.putExtra(Intent.EXTRA_SUBJECT,
"Sharing File...");
mIntent.putExtra(Intent.EXTRA_TEXT, "Sharing File...");
startActivity(Intent.createChooser(mIntent, "Share My File"));
}
It worked for me, try it. Good luck
File outputFile = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS), "path_of_file.pdf");
Uri uri = Uri.fromFile(outputFile);
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/pdf");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("org.telegram.messenger");
activity.startActivity(share);
Add android:requestLegacyExternalStorage="true" at manifest file.

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);

Add read_uri_permissions to share intent in Gluon Project

I'm trying to share an image and text with android intent ACTION_SEND:
public void shareContent(String text, File image) {
Item clipItem = new ClipData.Item(text);
ClipData clipData = new ClipData(null, new String[] {}, clipItem);
ClipboardManager clipBoardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipBoardManager.setPrimaryClip(clipData);
Uri pictureData = FileProvider.getUriForFile(context,
"mypackage.fileprovider",
image);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, pictureData);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(shareIntent, "Share content..."));
}
When I add the FLAG_GRANT_READ_URI_PERMISSIONS to the intent, I get an PermissionDenied error, when the according picture is getting accessed.
I can make it work with:
private void grantPicturePermissions(FXActivity context, Intent shareIntent, Uri pictureData) {
List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(shareIntent, 0);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
context.grantUriPermission(packageName, pictureData, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
But then I have to revoke all the granted permissions, when my app is resumed.
Is this an issue related to Gluon or what could be the reason?
(shareIntent.setClipData(clipData) didn't work either, thats why I had to use clipBoardManager.setPrimaryClip(clipData))

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"));

Share image with Android intent

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..."));
}

Categories

Resources