whatsapp和twitter没有在Android应用程序中获取图像

时间:2016-05-21 06:32:23

标签: android facebook twitter

我正在创建一个应用程序,我在社交媒体应用程序中发送图像和视频facebook g +正常工作但是whatsapp和twitter无法正常使用此代码

代码适用于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")));
            }
        }
    });

和推特代码是

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")));
            }
        }
    });

请帮助谢谢

1 个答案:

答案 0 :(得分:0)

试试这段代码,首先你的路径可能是错误的

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,"testing text");
  share.putExtra(Intent.EXTRA_STREAM, uri);
  share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  startActivity(share);

和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);

可能会对你有所帮助