Issue
I have an app that receives single or multiple image files from other apps via the Share feature. The single file share intent-filter works without problems but with the multiple file I am having some issues. If I select a combination of images and any other mime-type, say PDFs then the share option lists my app. I have written the intent-filter to accept only JPEGs and PNGs. If no images are selected in the list of files to be shared then my app is NOT listed but it's the combination of images and other files that is bothering me. Below is the intent-filter that I wrote.
<intent-filter android:icon="@mipmap/ic_print_file_receiver"
android:label="@string/app_name">
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/jpeg" />
<data android:mimeType="image/png" />
</intent-filter>
Can someone help me figure out the problem? I want to avoid my app being listed when we have non JPEG/PNGs selected. Appreciate the help!
Solution
After going through a lot of apps if found out that different apps are handling the implicit intent differently.
- I found that ESExplorer would completely disregard the intent-filter. If I pick two PDFs and share I would see my printer listed which was completely against my mime-type setting (shown above in my question).
- Another File Explorer app called BlackMoon file explorer, did the right thing and would show my app when I shared only when I had all the selected apps as JPEG or PNGs. My app does not get listed in the list of apps if I had even one file other than JPEG or PNG.
- Google download on my LG Nexus 5 did something strange. I app would not be listed if I shared after selecting only PDFs other anything other than images but it would list my app of I selected a combination. Like if I selected a JPEG, two PDFs, one.HTML file etc. All it needed was at least one JPEG or PNG in the list of files to be shared.
I had all these combinations of different behaviors. I think the best way to handle this was to gate the files inside my app. So here is what I wrote -
if (Intent.ACTION_SEND_MULTIPLE.equals(receivedIntent.getAction()) && receivedIntent.getType() != null){
String type = receivedIntent.getType();
ArrayList<Uri> filesListTemp = new ArrayList<>(filesList);
// MimeTypes.ALL is */* - the mimetype passed when multiple files are passed as the shared received intent. If only Images then it would be image/*.
if(TextUtils.equals(type, MimeTypes.ALL)){
// Here filesListTemp is getIntent().getExtras().getParcelableArrayList(Intent.EXTRA_STREAM);
for(Uri fileURI : filesListTemp){
String mimeType = getActivity().getContentResolver().getType(fileURI);
Log.d(TAG, "mimeType of the file : " + mimeType);
if(mimeType != null){
if(!TextUtils.equals(mimeType, MimeTypes.JPG) && !TextUtils.equals(mimeType, MimeTypes.PNG)){
filesList.remove(fileURI);
}
} else {
String filePath = fileURI.getPath().toLowerCase();
if (!filePath.endsWith(".jpg") && !filePath.endsWith(".jpeg") && !filePath.endsWith(".png")) {
filesList.remove(fileURI);
}
}
}
Don't forget to handle the case where all fileURIs is removed. Don't want to get NullPointers. Hope this helps somebody!
Answered By - luckylukein
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.