0

This is my code where i am trying to send image to whtsapp using intent but it gives file not found exception

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);
Dhruv
  • 1,862
  • 3
  • 20
  • 38
md gouse
  • 527
  • 1
  • 6
  • 22

1 Answers1

0

Shymaa Othman Answer here working fine as you want

You need to give the permission also in android mainfest

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

also for Android 6.0 and above need runtime permission of 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;
  }
}

call shareImageWhatsApp in class

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;
  }
}
Community
  • 1
  • 1
Arjun saini
  • 4,223
  • 3
  • 23
  • 51