我正在尝试将图像发送到whatsapp但是Toast出现并且说不支持文件格式化

时间:2016-06-22 18:51:13

标签: android-intent android-image whatsapp

我正在尝试将图片分享给whatsapp或其他应用。图像在android studio中的drawable文件夹中。但是当我尝试这样做时,Toast表示不支持文件格式化。以下是我的代码。请提前帮助谢谢

 public void share(View view)
{
    Uri uri=Uri.parse("android.resource://com.faisal.home.myapplication/drawable/"+R.drawable.tease);
    Intent intent,chooser;
    intent=new Intent(Intent.ACTION_SEND);
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_STREAM,uri);
    chooser=Intent.createChooser(intent,"share Image");
    if(intent.resolveActivity(getPackageManager())!=null)
    {
        startActivity(chooser);

    }
    else
    {

        Toast.makeText(this,"No app to share",Toast.LENGTH_LONG).show();
    }


}

1 个答案:

答案 0 :(得分:5)

Try below mention code its working fine:

 try {
                    Uri imageUri = null;
                    try {
                        imageUri = Uri.parse(MediaStore.Images.Media.insertImage(Activity.getContentResolver(),
                                BitmapFactory.decodeResource(getResources(), R.drawable.yourimage), null, null));
                    } catch (NullPointerException e) {
                    }

                    Intent shareIntent = new Intent(Intent.ACTION_SEND);
                    shareIntent.setType("*/*");
                    shareIntent.putExtra(Intent.EXTRA_TEXT, "your text");

                    shareIntent.putExtra(Intent.EXTRA_STREAM,imageUri);

                    startActivity(shareIntent);
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(Activity, "no app fount", Toast.LENGTH_LONG).show();
                }