最近的whatsapp更新不允许通过意图发送图像

时间:2016-09-13 09:18:18

标签: android android-intent sharing android-intentservice

这是我的代码,我试图使用intent将图像发送到whtsapp但是它给文件找不到异常

Uri uri = Uri.parse("android.resource://com.company.demoapp/" + resId);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "this is a new url");
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_STREAM,uri);
intent.setType("image/jpeg");
intent.setPackage("com.whatsapp");
mContext.startActivity(intent);

1 个答案:

答案 0 :(得分:0)

Shymaa Othman Answer here working fine as you want

您还需要在android mainfest中提供权限

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

对于Android 6.0及更高版本也需要WRITE_EXTERNAL_STORAGE的运行时权限

private static final int REQUEST_RUNTIME_PERMISSION = 123;

 if (CheckPermission(youactivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

      shareImageWhatsApp();


    } else {
      // you do not have permission go request runtime permissions
      RequestPermission(AccesspointCheck.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION);
    }




public void RequestPermission(Activity thisActivity, String Permission, int Code) {
  if (ContextCompat.checkSelfPermission(thisActivity,
          Permission)
          != PackageManager.PERMISSION_GRANTED) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Permission)) {
      shareImageWhatsApp();


    } else {
      ActivityCompat.requestPermissions(thisActivity,
              new String[]{Permission},
              Code);
    }
  }
}

public boolean CheckPermission(Activity context, String Permission) {
  if (ContextCompat.checkSelfPermission(context,
          Permission) == PackageManager.PERMISSION_GRANTED) {
    return true;
  } else {
    return false;
  }
}

在类

中调用shareImageWhatsApp
public void shareImageWhatsApp() {

  Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.launcher_icon);
  Intent share = new Intent(Intent.ACTION_SEND);
  share.setType("image/jpeg");
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  File f = new File(Environment.getExternalStorageDirectory()
          + File.separator + "temporary_file.jpg");
  try {
    f.createNewFile();
    new FileOutputStream(f).write(bytes.toByteArray());
  } catch (IOException e) {
    e.printStackTrace();
  }
  share.putExtra(Intent.EXTRA_STREAM,
          Uri.parse( Environment.getExternalStorageDirectory()+ File.separator+"temporary_file.jpg"));
  if(isPackageInstalled("com.whatsapp",this)){
    share.setPackage("com.whatsapp");
    startActivity(Intent.createChooser(share, "Share Image"));

  }else{

    Toast.makeText(getApplicationContext(), "Please Install Whatsapp", Toast.LENGTH_LONG).show();
  }

}

private boolean isPackageInstalled(String packagename, Context context) {
  PackageManager pm = context.getPackageManager();
  try {
    pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
    return true;
  } catch (PackageManager.NameNotFoundException e) {
    return false;
  }
}