Not working Share Image in ShareIntent In android - android

I used ShareIntent in android .i want share bitmap image in facebook via shareintent.my code below . how to slove this question.
Thanks!!!
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Facebook") {
Uri pngUri = Uri.parse("file//res/drawable/camera.png");
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"YOUR TEXT HERE");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,"YOUR TEXT HERE");
shareIntent.putExtra(android.content.Intent.EXTRA_STREAM,pngUri);
PackageManager pm =getActivity().getApplicationContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("facebook")) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(
activity.applicationInfo.packageName,
activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
startActivity(shareIntent);
}
}
}
else if (item.getTitle() == "Twitter") {
Toast.makeText(getActivity().getApplicationContext(), "Twitter",
1).show();
} else {
return false;
}
return true;
}

try this
//add code on save button
File file;
Bitmap bitmap=((BitmapDrawable)imageview.getDrawable()).getBitmap();
ByteArrayOutputStream stream1=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream1);
byte[] imageInByte2=stream1.toByteArray();
try{
String path = Environment.getExternalStorageDirectory().toString();
File imgDirectory = new File(Environment.getExternalStorageDirectory()+"/Cards/");
imgDirectory.mkdirs();
OutputStream fOut = null;
file = null;
file = new File(path,"/Cards/"+etcardname.getText().toString()+ ".png");
Toast.makeText(getActivity(), "saved at: " + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
}catch(Exception e){
e.printStackTrace();
}
//share image code
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file));
startActivity(Intent.createChooser(share, "Share image using"));

Related

How do I share Images from Firebase Database?

I have created database and there is Url of image and I want to show that image in my app which is working but now I want share that image in Whatsapp etc. Now what I need to change there as I didn't get any idea,how to do it?
Here is my Code:
#Override
public void onBindViewHolder(#NonNull final myViewHolder holder, int position) {
Picasso.get().load(mdata.get(holder.getAdapterPosition()).getImage()).into(holder.imgView);
holder.shareBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent;
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), holder.imgView.getId());
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) +"/share.png";
FileOutputStream out = null;
File file = new File(path);
try{
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
out.flush();
out.close();
}catch(Exception e){
e.printStackTrace();
}
path = file.getPath();
Uri bmpUri = Uri.parse("file://" + path);
intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, bmpUri);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_TEXT," Hello Your's Images ");
context.startActivity(Intent.createChooser(intent, " Share Images Via"));
You can do it using uri, and try to implement the next code:
Uri imageUri = Uri.parse(pictureFile.getAbsolutePath());
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//Send to Whattssap
shareIntent.setPackage("com.whatsapp");
//Even you can add text to the image
shareIntent.putExtra(Intent.EXTRA_TEXT, picture_text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
And that's all

How to share GIF image in Whatsapp programmatically in Android?

WhatsApp have added new feature display GIF.
if any one know how to share GIF in whatsapp then let me know
Try this....
private void shareGif(String resourceName){
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "sharingGif.gif";
File sharingGifFile = new File(baseDir, fileName);
try {
byte[] readData = new byte[1024*500];
InputStream fis = getResources().openRawResource(getResources().getIdentifier(resourceName, "drawable", getPackageName()));
FileOutputStream fos = new FileOutputStream(sharingGifFile);
int i = fis.read(readData);
while (i != -1) {
fos.write(readData, 0, i);
i = fis.read(readData);
}
fos.close();
} catch (IOException io) {
}
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/gif");
Uri uri = Uri.fromFile(sharingGifFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share Emoji"));
}
For share gif on whatsapp
/* Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.whatsapp");
shareIntent.putExtra(Intent.EXTRA_TEXT,title + "\n\nLink : " + link );
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(sharePath));
shareIntent.setType("image/*");
startActivity(shareIntent);*/
You just need to set the type of image intent as gif like below
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/gif");

Image get stored in internal storage while sharing images in android

Im creating on application in that application am sharing layout like image. while am sharing am converting layout into image and sharing in social network, but issues is image getting stored in internal storage, how to delete after sharing an image.
bitmap = Bitmap.createBitmap(itemView.getWidth(), itemView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
itemView.draw(canvas);
try {
// output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/path/to/file.png");
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
image_path = file.getAbsolutePath();
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 50, out);
out.close();
bmpUri = Uri.fromFile(file);
sharePost();
public void sharePost() {
try {
Resources resources = context1.getResources();
final String photoUri = MediaStore.Images.Media.insertImage(
context1.getContentResolver(), image_path, null, null);
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
emailIntent.setType("image/*");
PackageManager pm = context1.getPackageManager();
Intent openInChooser = Intent.createChooser(emailIntent, "Share via Oddcast");
List<ResolveInfo> resInfo = pm.queryIntentActivities(emailIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if (packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
} else {
emailIntent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
if (packageName.contains("twitter")) {
emailIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
} else if (packageName.contains("facebook")) {
emailIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
} else if (packageName.contains("mms")) {
emailIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
} else if (packageName.contains("android.gm")) {
emailIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
} else if (packageName.contains("whatsapp")) {
emailIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
}else if (packageName.contains("com.google.android.apps.plus")) {
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(photoUri));
}
else{
emailIntent.putExtra(Intent.EXTRA_STREAM, MyCustomAdapter.bmpUri);
}
intentList.add(new LabeledIntent(emailIntent, packageName, ri.loadLabel(pm), ri.icon));
}
}
// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
context1.startActivity(openInChooser);
Log.e("start", "start");
}catch (SecurityException e) {
Toast.makeText(context1, "Please check your permissions settings.Permission issue.", Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (Exception e) {
Toast.makeText(context1, "Please check your permissions settings.Permission issue.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
file.delete();
Log.e("emd","end");
//context1.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(String.valueOf(bmpUri)))));
}
Instead of startActivity try to call startActivityForResult. Overide the onActivityResult method and delete the image file you created

How can I send bitmap to WhatsApp Application

My question how can I send bitmap to Whastapp Application and I use below code;
ImageView iv=(ImageView)view.findViewById(R.id.item_image);
Bitmap bitmap = ((BitmapDrawable)iv.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.setType("image/png");
waIntent.putExtra(Intent.ACTION_SEND, byteArray);
startActivity(Intent.createChooser(waIntent, "Share with"));
But that code did not work. What is my error ? Thanks.
This worked for me:
public void onClickApp(String pack, Bitmap bitmap) {
PackageManager pm = context.getPackageManager();
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "Title", null);
Uri imageUri = Uri.parse(path);
#SuppressWarnings("unused")
PackageInfo info = pm.getPackageInfo(pack, PackageManager.GET_META_DATA);
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("image/*");
waIntent.setPackage(pack);
waIntent.putExtra(android.content.Intent.EXTRA_STREAM, imageUri);
waIntent.putExtra(Intent.EXTRA_TEXT, pack);
context.startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (Exception e) {
Log.e("Error on sharing", e + " ");
Toast.makeText(context, "App not Installed", Toast.LENGTH_SHORT).show();
}
}
//pass your image and text(if you want to share) in this method.
void shareImage(Bitmap bitmap,String text){
//bitmap is ur image and text is which is written in edtitext
//you will get the image from the path
String pathofBmp=
MediaStore.Images.Media.insertImage(getContentResolver(),
bitmap,"title", null);
Uri uri = Uri.parse(pathofBmp);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Star App");
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "hello hello"));
}

Sharing images in other application

I have application with images and I want share image which is choosen by user in some other application. From other question here I know that I must put the image in public place so it can be accesed by other application. But I still get error "no application can perform this action" any idea where am I doing mistake?
Code for copying image to SD card:
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path,String.valueOf(idOfImage));
if (!file.exists())
{
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),idOfImage);
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
myBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e)
{
e.printStackTrace();
} finally {
try {
if (out != null)
{
out.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
Code for sending the intent and picking chooser:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
Uri uri = Uri.fromFile(file);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
StartActivity(Intent.createChooser(shareIntent,getResources().getText(R.string.share)));
Thanks for answers.
EDIT: Works fine when sharingIntent.setType("image/png"); lane added, with Gmail and G+ , but doesnt work with Messengers FB and others.
try this code
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String)
v.getTag(R.string.app_name));
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); // put your image URI
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("facebook"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Edit 1
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(filePath))); //optional//use this when you want to send an image
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
use this code i tried it and it works here at my side
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(path);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));

Categories

Resources