1. Are you ready for the Galaxy S20? Here is everything we know so far!

Sharing image on Whatsapp : Android

Discussion in 'Android Development' started by tapariasud, Sep 3, 2021.

  1. tapariasud

    tapariasud Lurker
    Thread Starter

    Hi I am trying to create a simple android code of sharing QR code image via Whatsapp.But even though QR code is being properly generated and image is being also stored in the emulator, the image share to Whatsapp(via Intent) doesnt happen and the application crashes in phone but works in emulator.Here below is my code snippet.

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
    {
    super.onCreateContextMenu(menu, v, menuInfo);
    // you can set menu header with title icon etc
    menu.setHeaderTitle("Share It");
    // add menu items
    menu.add(0, v.getId(), 0, "Share on Whatsapp");
    menu.add(0, v.getId(), 0, "Share on Email");


    }
    public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
    }

    @override
    public boolean onContextItemSelected(MenuItem item) {

    if (item.getTitle() == "Share on Whatsapp")
    {
    Uri imageUri=getImageUri(getApplicationContext(),bitmap);
    Toast.makeText(getApplicationContext(),imageUri.getPath().toString(),Toast.LENGTH_SHORT).show();
    // Log.e("Check",imageUri.getPath().toString());
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    //shareIntent.putExtra(Intent.EXTRA_TEXT,title + "\n\nLink : " + link );
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imageUri.getPath()));
    shareIntent.setType("image/*");
    startActivity(Intent.createChooser(shareIntent, "Share image via:"));
    //Uri imageUri = Uri.parse(pictureFile.getAbsolutePath());
    /* 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, picture_text);
    shareIntent.putExtra(Intent.EXTRA_STREAM,imageUri);
    shareIntent.setType("image/jpeg");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);*/

    try {
    startActivity(shareIntent);
    } catch (android.content.ActivityNotFoundException ex)
    {

    //Toast.makeText(getApplicationContext(),"Exception "+ex.getMessage().toString(),Toast.LENGTH_SHORT).show();
    Log.e("Error",ex.getMessage().toString());
    //Toast.makeText("Whatsapp not installed",Toast.LENGTH_SHORT).show();
    //Toast.makeText("Whatsapp not installed", Toast.LENGTH_SHORT).show();
    }
    }
    else if (item.getTitle() == "Share on Email")
    {
    ;
    }
    return true;
    }

    Here is the crash report on phone

    java.lang.NullPointerException: uriString
    at android.net.Uri$StringUri.<init>(Uri.java:490)
    at android.net.Uri$StringUri.<init>(Uri.java:480)
    at android.net.Uri.parse(Uri.java:452)
    at com.sudarshan.qrcodegenerator.MainActivity.getImageUri(MainActivity.java:109)
    at com.sudarshan.qrcodegenerator.MainActivity.onContextItemSelected(MainActivity.java:117)
    at android.app.Activity.onMenuItemSelected(Activity.java:3628)
    at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:436)
    at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:196)
    at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
    at com.android.internal.policy.PhoneWindow$PhoneWindowMenuCallback.onMenuItemSelected(PhoneWindow.java:3750)
    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:776)
    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:148)
    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:923)
    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:913)
    at com.android.internal.view.menu.MenuPopup.onItemClick(MenuPopup.java:128)
    at android.widget.AdapterView.performItemClick(AdapterView.java:318)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1165)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3145)
    at android.widget.AbsListView.onTouchUp(AbsListView.java:4077)
    at android.widget.AbsListView.onTouchEvent(AbsListView.java:3831)
    at android.widget.DropDownListView.onTouchEvent(DropDownListView.java:116)
    at android.view.View.dispatchTouchEvent(View.java:12558)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3079)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2727)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3085)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2741)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3085)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2741)
    at android.widget.PopupWindow$PopupDecorView.dispatchTouchEvent(PopupWindow.java:2407)
    at android.view.View.dispatchPointerEvent(View.java:12804)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5428)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5219)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4730)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4783)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4749)
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Sharing image Whatsapp
  1. App Update
    Replies:
    0
    Views:
    249
  2. TuxPhone
    Replies:
    4
    Views:
    261
  3. ixoni
    Replies:
    8
    Views:
    720
  4. The_Chief
    Replies:
    2
    Views:
    385
  5. Emir Sanchez
    Replies:
    1
    Views:
    693
  6. MOTE14
    Replies:
    3
    Views:
    483
  7. App Update
    Replies:
    1
    Views:
    537
  8. App Update
    Replies:
    0
    Views:
    461
  9. dakshay95
    Replies:
    6
    Views:
    1,142
  10. tuscan72
    Replies:
    0
    Views:
    684

Share This Page

Loading...