Returning an Image to whatsapp

*爱你&永不变心* 提交于 2019-11-28 00:23:43

After searching for days, here is a working solution to return images to all other applications (tested for GMail and WhatsApp).

First, you need to set an intent-filter in your AndroidManifest.xml (Inside application > activity). This will list your application when other apps are calling for this intent (like when requesting an image). Note: WhatsApp is using the action.PICK - intent. Adding all intent-filters below even though will provide great compatibility with other apps.

<intent-filter>
                <action android:name="android.intent.action.PICK" /> 
                <category android:name="android.intent.category.DEFAULT"  /> 
                <category android:name="android.intent.category.OPENABLE" />
                <data android:mimeType="image/*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.OPENABLE" />
                <data android:mimeType="image/*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.GET_CONTENT" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.OPENABLE" />
                <data android:mimeType="image/*" />
            </intent-filter>

The second thing you need to care for is responding to an idling intent. This should consist of two parts: First you should check whether your application has been executed to return an image or if its run all by itself.

Intent intent = getIntent();
        if (intent!=null && intent.getType()!=null)  //check if any application has executed your app
        {
             if(intent.getType().indexOf("image/") != -1) isinint=true; //check if the requested type is an image. If true, set a public static boolean, f.e. named isinint to true. Default is false.
        }

Now, when the user has picked an image, set the result as following. Due to memory issues, you should copy the file you want to return onto the sdcard and return the Uri.

if(isinint) //check if any app cares for the result
        {
            Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND, Uri.fromFile(openprev)); //Create a new intent. First parameter means that you want to send the file. The second parameter is the URI pointing to a file on the sd card. (openprev has the datatype File)

            ((Activity) context).setResult(Activity.RESULT_OK, shareIntent); //set the file/intent as result
            ((Activity) context).finish(); //close your application and get back to the requesting application like GMail and WhatsApp
            return; //do not execute code below, not important
        }

Note!: You can leave out ((Activity) context) when calling the data in OnCreate or similiar void's. As i use this snippet in another void, i need to provide a context in any case that has to be defined as displayed.

Followig is working for me:

    <activity android:name="com.selcuksoydan.sorucevap.Main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
            <data android:mimeType="text/*" />
        </intent-filter>



        Uri imageUri = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
        if (imageUri != null) {
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!