I can share image and text in Facebook messenger but not in Facebook. How to do that? I am using the following code.
Intent sharingIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sharingIntent.setType("image/jpg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Body text of the new status");
context.startActivity(Intent.createChooser(sharingIntent, "Share using"));
Facebook does not currently support:
EXTRA_TEXT besides URLs
EXTRA_STREAM from external sources (only local files)
Until Facebook supports those features, you are limited to only local images without any text.
Try out as below :
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/OnItsOwn/";
File dir = new File(file_path);
dir.mkdirs();
File file = new File(dir, "sample.jpg");
FileOutputStream fOut = new FileOutputStream(file);
screenshot.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
// Share
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
share.putExtra(Intent.EXTRA_TEXT, "Body text of the new status");
startActivity(Intent.createChooser(share, "Share Image"));
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 am try to send pdf file from my app to whatsapp, and here is the code,
but something missing!!
it opens whatsapp and i can choose a contact but it says "sharing failed"!
the code
String PLACEHOLDER = "file:///android_asset/QUOT_2016_10(test).pdf";
File f = new File(PLACEHOLDER);
Uri uri = Uri.fromFile(f);
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_TEXT, "hi");
share.setPackage("com.whatsapp");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("application/pdf");
activity.startActivity(share);
I figured out the problem, and here is the answer if somebody had the same issue. The problem was that I am trying to open the pdf from the asset folder which did n't work, and if would try to open the pdf from the download folder for example, it would work. Please refer to the the code below for the final correct way:
File outputFile = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS), "ref Number from Quotation.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("com.whatsapp");
activity.startActivity(share);
File outputPath= new File(Environment.getExternalStorageDirectory() + "/MyPdf/example.pdf");//change with your path
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
Uri fileUri = FileProvider.getUriForFile(getApplicationContext(),
getPackageName() + ".fileprovider", outputPath);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
startActivity(Intent.createChooser(shareIntent, "Share it"));
It's technically wrong, what if someone has WhatsApp business or want to share file on gmail then use this...
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, _text);
shareIntent.putExtra (Intent.EXTRA_STREAM, Uri.parse(_file));
startActivity(Intent.createChooser( shareIntent, "Share"));
In this u just have to add text and file
Text u attach will become subject in gmail and if you are sharing image on WhatsApp then text will become as image caption
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 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);
i am attempting to save an image file using "openFileOutput" and then adding that file to my intent with EXTRA_STREAM. but logcat keeps saying that file size is 0, i have the proper permission in my manifest.
FileOutputStream fos = openFileOutput("p001.jpg", Context.MODE_WORLD_READABLE);
File jpg = getFileStreamPath("p001.jpg");
fos.close();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_SUBJECT, "Fail picture");
share.putExtra(Intent.EXTRA_TEXT, "Epic fail!!!");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(jpg));
startActivity(Intent.createChooser(share, "Choose share method."));
updated code
private void HandleSMS() throws IOException {
FileOutputStream fos = openFileOutput(R.drawable.p001, Context.MODE_WORLD_READABLE);
File jpg = getFileStreamPath("p001.jpg");
fos.close();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_SUBJECT, "Fail picture");
share.putExtra(Intent.EXTRA_TEXT, "Epic fail!!!");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(jpg));
startActivity(Intent.createChooser(share, "Choose share method."));
}
So, do one thing instead of the direct name use R.drawable.NameOfTheImage.