Skip to content Skip to sidebar Skip to footer

How To Share Image To Whatsapp From Gallery Programmatically?

I tried the following code to share an image to WhatsApp. For now, I manually added the image path. I want to open the gallery when the user clicks a button in my application and h

Solution 1:

You can use the below code to dynamically get the image Uri. It will work on all versions of Android.

MainActivity

publicclassMainActivityextendsActivity {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Intentintent=newIntent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent, 0);
    }

    @OverrideprotectedvoidonActivityResult(int reqCode, int resCode, Intent data) {
        if(resCode == Activity.RESULT_OK && data != null){
            String realPath;

            if (Build.VERSION.SDK_INT < 11)
                realPath = RealPathUtil.getRealPathFromURI_BelowAPI11(this, data.getData());


            elseif (Build.VERSION.SDK_INT < 19)
                realPath = RealPathUtil.getRealPathFromURI_API11to18(this, data.getData());


            elserealPath= RealPathUtil.getRealPathFromURI_API19(this, data.getData());


            getImageInfo(Build.VERSION.SDK_INT, data.getData().getPath(),realPath);
        }
    }

    privatevoidgetImageInfo(int sdk, String uriPath,String realPath){

        UriuriFromPath= Uri.fromFile(newFile(realPath));
        Log.d("Log", "Build.VERSION.SDK_INT:"+sdk);
        Log.d("Log", "URI Path:"+uriPath);
        Log.d("Log", "Real Path: "+realPath);
    }
}

RealPathUtil.java

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

publicclassRealPathUtil {

    @SuppressLint("NewApi")publicstatic String getRealPathFromURI_API19(Context context, Uri uri){
        StringfilePath="";
        StringwholeID= DocumentsContract.getDocumentId(uri);

         // Split at colon, use second item in the arrayStringid= wholeID.split(":")[1];

         String[] column = { MediaStore.Images.Media.DATA };     

         // where id is equal to             Stringsel= MediaStore.Images.Media._ID + "=?";

         Cursorcursor= context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                                   column, sel, newString[]{ id }, null);

         intcolumnIndex= cursor.getColumnIndex(column[0]);

         if (cursor.moveToFirst()) {
             filePath = cursor.getString(columnIndex);
         }   
         cursor.close();
         return filePath;
    }


    @SuppressLint("NewApi")publicstatic String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
          String[] proj = { MediaStore.Images.Media.DATA };
          Stringresult=null;

          CursorLoadercursorLoader=newCursorLoader(
                  context, 
            contentUri, proj, null, null, null);        
          Cursorcursor= cursorLoader.loadInBackground();

          if(cursor != null){
           intcolumn_index= 
             cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
           cursor.moveToFirst();
           result = cursor.getString(column_index);
          }
          return result;  
    }

    publicstatic String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){
               String[] proj = { MediaStore.Images.Media.DATA };
               Cursorcursor= context.getContentResolver().query(contentUri, proj, null, null, null);
               intcolumn_index= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
               cursor.moveToFirst();
               return cursor.getString(column_index);
    }
}

Add this Permission to Manifest.xml

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

In Android 6+, You need to request the Permission during RunTime.

Post a Comment for "How To Share Image To Whatsapp From Gallery Programmatically?"