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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…