Reputation: 263
I have to share image with text to all social medias. So I tried below code:-
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = imageUrl;
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String)
v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"Text for post");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(shareIntent, "Share Using"));
}
});
It is working, now I can share text with image in gmail, like apps. But the problem is I can't getting Facebook, Twitter and Instagram by using this shared intent even though I have installed and updated all this apps.
I need to get all the social media apps for sharing.
By using "text/plain" as shareIntent type Facebook appears but can't share image...
Can someone help me to find the answer?
Thanks in advance.
Upvotes: 1
Views: 1143
Reputation: 5134
Try below code:
public void share(final String url, final String text) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(
Intent.EXTRA_TEXT,
"Sharing from "
+ context.getString(R.string.app_name)
+ "\n" + text);
if (url != null) {
Bitmap bmp = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
byte[] chunk = new byte[4096];
int bytesRead;
InputStream stream = new URL(url).openStream();
while ((bytesRead = stream.read(chunk)) > 0) {
outputStream.write(chunk, 0, bytesRead);
}
outputStream.toByteArray();
bmp = BitmapFactory.decodeByteArray(
outputStream.toByteArray(), 0,
outputStream.toByteArray().length);
} catch (IOException e) {
e.printStackTrace();
Log.v("Error", e.toString());
}
String filename = Environment.getExternalStorageDirectory().getAbsoluteFile() + File.separator + File.separator
+ Utils.getCurrentTimeInFormate() + ".png";
Log.e("BITMAP", filename);
FileOutputStream out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 50, out);
Bitmap icon = bmp;
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "title");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
OutputStream outstream;
try {
outstream = context.getContentResolver()
.openOutputStream(uri);
icon.compress(Bitmap.CompressFormat.PNG, 60, outstream);
outstream.close();
} catch (Exception e) {
System.err.println(e.toString());
}
share.putExtra(Intent.EXTRA_STREAM, uri);
}
context.startActivity(Intent.createChooser(share, "Share"));
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
Upvotes: 0
Reputation: 6622
Try this its work for me,
void share(String nameApp, Uri imagePath) {
boolean isAppExist = false;
try {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(Intent.ACTION_SEND);
targetedShare.setType("image/*"); // put here your mime type
if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_title));
targetedShare.putExtra(Intent.EXTRA_TEXT, shareMessage);
if (imagePath != null)
targetedShare.putExtra(Intent.EXTRA_STREAM, imagePath);
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
isAppExist = true;
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivityForResult(chooserIntent, REQUEST_SHARE);
}
} catch (Exception e) {
Utils.setLog("Exception while sending image on" + nameApp + " " + e.getMessage());
}
if (!isAppExist) {
Dialogs.showAlert(this, null, getString(R.string.share_no_application_found), true, false);
}
}
in nameApp
you have to pass name of application in which you have to post image. In your case pass facebook
.
Upvotes: 0