How to share image and text with custom formatting? - android

I am able to share image and multiple text using the following code:
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("*/*");
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(android.content.Intent.EXTRA_SUBJECT, "this is subject");
share.putExtra(Intent.EXTRA_TEXT, "HeadLine.\nIntroText");
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
I tested it in whatsapp share and it is working. Here is the current share output:
-->> Image display
-->> Subject display
-->> Extra text display
But I am using custom formatting and it is not working. Here is the custom formatting:
-->> Image display in left side with 20*20 -->> Subject display in right side
-->> Extra text display in right side
Can this be done?

Related

How to implement an Image Slider that becomes full screen when clicked?

How can I add an image slider that becomes full screen when clicked?
I would like to be able to preview the images in full screen mode and be able to share it or zoom it. What are some ways that I can implement this?
Use this lib to zoom image and for sharing an image you can use share intent
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"));

Share content to Messenger or Line or other like Facebook or Tweeter

Hi I am new to develop application in android.
I saw other apps, they use share to friends with Messenger, or Facebook..
for instant, I share it to Messenger, then in the content should show some texts and the link of app!!
How can I do that? anyone please help!!!
I have tried the answer in this site to share, but it works only with SMS and Gmail, but if I share to Messenger, it shows nothing!
Try this code for sharing drawable image with link and text.
Follow this steps
Step 1 :- Take one button in your layout.xml for sharing your content.
Step 2 :- set onClickListner for this button in your Activity.java.
Step 3 :- Put this code to your Activity.java file Button setOnClickListner.
String myText = "share text though this string";
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.mipmap.icon_256);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpg");
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();
}
String extraText = myText + "\n\nhttps://www.google.co.in";
share.putExtra(Intent.EXTRA_TEXT,extraText);
share.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(f));
startActivity(Intent.createChooser(share, "Share Image"));

Not able to share an Image on Whatsap

I am writing an app to share image.
I wrote the code but getting problems.
What I am doing
1: Saving the image temporarily in Internal Stoarge
2: Passing the URI to share the image.
Image is successfully saved in internal storage but not getting share on whatsapp.
When I share it on whatsapp, Whatsapp get opens, I select recipient, whatsapp processes it and says "Retry".
Code:
public void shareImage(View V)
{
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.download);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "myDownloadedImage.jpg");
try
{
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
Toast.makeText(this, "Some error in Writing"+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Uri downloadLocation=Uri.fromFile(file);
share.putExtra(Intent.EXTRA_STREAM, downloadLocation);
startActivity(Intent.createChooser(share, "Share Image"));
}
Image succesfully get saved on internal storage but not getting shared on whatsapp.
The screenshot is below. We can see images are not shared successfully.
Use below code to fetch image and make uri:
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();
bmpUri = Uri.fromFile(file);
Now pass the bmpUri in:
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
This should make your image share.

How to share image and text to facebook app

I am trying to share an image and text.
When I'm doing this:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
screenshot.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"));
share.putExtra(android.content.Intent.EXTRA_TEXT, textToShare);
startActivityForResult(Intent.createChooser(share, "Share Image"),
FROM_SHARE);
It works greate on all apps (that was choosen from the popup dialog like Gmail) except for facebook, facebook takes only the image and not the text.
Is there a whay to pass an extra text with an image to the facebook app?
Thanks!
There is a bug in FACEBOOK I have the same problem If you really want to do that anyhow you need to use facebook SDK for that and use Facebook ShareDialog.

Android Intent Sharing From Gridview

Creating an a app, where I store bunch of photos in the drawable folder, When the user longpress on a photo brings up a menu, one of the options is to share.I can get the contextmenu up, but have no clue how to share from the grid menu.
I also have the expand it is bring up single image in own view and can share from there.
But when the user selects the Image from the gridview I have no Idea how to share that one.
this is the method i call when user selects share option
public Intent intentShare() {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://your.package.here/drawable/"+share.putExtra("id", lastPosition)));
startActivity(Intent.createChooser(share, "Share Image"));
return share;
}
I have seen this possible option/solution on a couple of other questions
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"));
But I have no idea how to initialize mBitmap if that works ?
The entire code can be found here: http://pastie.org/private/gdpyn2t55h0gp1vp1kika
You can convert the Bitmap from resource using code like this:
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), mThumbIds[lastPosition]);
now you can share it as you share in Expanded view.

Categories

Resources