nEwDeV nEwDeV - 6 months ago 51
Android Question

whatsapp and twitter is not getting images in android app

i am create an app where i am sending images and vedio in social media apps facebook g+ is working proper but whatsapp and twitter is not working i am using this code

code is for whatsapp

Button whtsapp_sahre = (Button)dialog.findViewById(R.id.whatsapp_btn);
whtsapp_sahre.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri imageUri = Uri.parse("android.resource://\" + getPackageName() + \"/drawable/pic8");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//Target whatsapp:
shareIntent.setPackage("com.whatsapp");
//Add text and then Image URI
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello");
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try{
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.whatsapp")));
}
}
});


and twitter code is

Button _Twwiterbtn = (Button) dialog.findViewById(R.id.twwiter_btn);
_Twwiterbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM,R.drawable.pic8);
shareIntent.putExtra(Intent.EXTRA_TITLE,"HELLO status");
shareIntent.setType("image/*")
.setPackage("com.twitter.android");
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.twitter.android&hl=en")));
}
}
});


please help thanks

Answer

try this code its help you first your path is may be wrong

code for whatsapp

 String imagePath =Environment.getExternalStorageDirectory() + "/img.jpg";
  File f=new File(imagePath);
  Uri uri = Uri.fromFile(f);
  Intent share = new Intent(Intent.ACTION_SEND);
  share.setPackage("com.whatsapp");
  share.setType("image/*");
  share.putExtra(Intent.EXTRA_TEXT,"Hello Basliyal ji");
  share.putExtra(Intent.EXTRA_STREAM, uri);
  share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  startActivity(share);

and code for twitter

      String imagePath = Environment.getExternalStorageDirectory()
                            + "/img.jpg";
            File f=new File(imagePath);
            Uri uri = Uri.fromFile(f);
            Intent share = new Intent(Intent.ACTION_SEND);
            share.setPackage("com.twitter.android");
            share.putExtra(Intent.EXTRA_TEXT, "Hello Amit Basliyal");
            share.putExtra(Intent.EXTRA_STREAM, uri);
            share.setType("image/*");
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(share);

may be help you thanks

Comments