【问题标题】:Recent whatsapp update doesnt allow to send image through intent最近的 whatsapp 更新不允许通过意图发送图像
【发布时间】:2016-09-13 09:18:17
【问题描述】:

这是我的代码,我试图使用意图将图像发送到 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);

【问题讨论】:

  • 确保您的文件路径正确且文件是否存在。
  • @md gouse 你试试我的回答吗
  • @PriyankPatel 文件路径已正确定义
  • 不,请帮助...@Arjunsaini

标签: android android-intent sharing android-intentservice


【解决方案1】:

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

【讨论】:

  • 你传入参数的REQUEST_RUNTIME_PERMISSION是什么。
  • 私有静态最终 int REQUEST_RUNTIME_PERMISSION = 123;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-27
  • 2021-11-14
  • 2015-06-01
  • 2017-11-11
  • 1970-01-01
相关资源
最近更新 更多