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.
Related
This my code: ` File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "/sample.pdf");
Uri uri = FileProvider.getUriForFile(
MainActivity.this,
"com.example.homefolder.example.provider", //(use your app signature + ".provider" )
outputFile);
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/pdf");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TEXT,"message content");
share.putExtra(Intent.EXTRA_SUBJECT,"mesaage subject");
startActivity(Intent.createChooser(share, "example text"));`
But when I share it only share pdf file, cannot share text content
I need to share 2 csv files I created in my app.
I was able to share one of them with the below code, but how to I pass 2 files to share?
for (File file : fileList) {
if (!file.exists()) {
Toast.makeText(ExcelExportActivity.this, "File doesn't exists", Toast.LENGTH_LONG).show();
return;
}
Uri uri = FileProvider.getUriForFile(ExcelExportActivity.this, "com.example.farmers.provider", file);
grantUriPermission(ExcelExportActivity.this.getPackageName(), uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.setType("text/csv");
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
}
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "my subject");
startActivity(Intent.createChooser(sharingIntent, "sharing with..."));
You will need to use Intent.ACTION_SEND_MULTIPLE instead of Intent.ACTION_SEND.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, "Here are some files.");
intent.setType("text/csv");
ArrayList<Uri> files = new ArrayList<Uri>();
for(String path : filesToSend /* List of the files you want to send */) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
files.add(uri);
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
startActivity(intent);
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 😅
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"));
Here i am sharing images from my app to whatsapp.but this code is working here only for mylist1[i] and not for mylist2[i] and mylist3[i]. As in my activity file there are 15 images in every list. what to do?
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
Uri uri = Uri.parse("android.resource://com.example.drawcelebrities/"+mylist1[i]+mylist2[i]+mylist3[i]);
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Share via"));
Take image array in mylist[] and use below code, then share image via whatsapp.
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+mylist[i]);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share via"));
If I'm not wrong then for that you should use android.content.Intent.ACTION_SEND_MULTIPLE..Refer this link it will help you..
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, "Here are some files.");
intent.setType("image/jpeg");
ArrayList<Uri> files = new ArrayList<Uri>();
for(String path : filesToSend /* List of the files you want to send */) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
files.add(uri);
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
startActivity(intent);