brian
brian

Reputation: 6912

How to filter apps while share photo on Android

I use below code to share photo by other apps and print their package name:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(FilePath));
PackageManager packageManager = this.getPackageManager();
List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(sharingIntent, PackageManager.MATCH_DEFAULT_ONLY);
int i = 0;
while(i < resolveInfo.size()) {
    System.out.println(i + "  " + resolveInfo.get(i).activityInfo.packageName);
    i++;
}
startActivity(Intent.createChooser(sharingIntent, "Share");

And get below 9 package name which can share photo:

06-19 16:55:22.460: I/System.out(13020): 0  com.amazon.kindle
06-19 16:55:22.460: I/System.out(13020): 1  com.android.bluetooth
06-19 16:55:22.460: I/System.out(13020): 2  com.google.android.apps.uploader
06-19 16:55:22.460: I/System.out(13020): 3  com.ecareme.asuswebstorage
06-19 16:55:22.460: I/System.out(13020): 4  com.google.android.talk
06-19 16:55:22.460: I/System.out(13020): 5  com.google.android.gm
06-19 16:55:22.460: I/System.out(13020): 6  com.aripollak.picturemap
06-19 16:55:22.460: I/System.out(13020): 7  com.instagram.android
06-19 16:55:22.460: I/System.out(13020): 8  com.facebook.katana

If I want to filter some apps while select app.
For example, I want to filter facebook app from share app list.
How can I dd?

Upvotes: 2

Views: 419

Answers (1)

brian
brian

Reputation: 6912

I found the method, but not sure is it the best method.
My method as below:
First, remove the line

startActivity(Intent.createChooser(sharingIntent, "Share");

Second, I remove the app I want to filter from list resolveInfo as below:

for(i = resolveInfo.size(); i >= 0; i--) {
    if((resolveInfo.get(i).activityInfo.packageName).equals("com.facebook.katana")) {
        resolveInfo.remove(i);
    }
}

Third, create own app chooser to show a ListView Dialog to show resolveInfo's item, resolveInfo.get(i).activityInfo.loadLabel(pm) and resolveInfo.get(i).activityInfo.loadIcon(pm), the app name and icon resid.

Finally, if click item i, then use below code to share photo:

Intent mysharingIntent = new Intent(Intent.ACTION_SEND);
mysharingIntent.setType("image/jpeg");
mysharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(FilePath));
mysharingIntent.setPackage(resolveInfo.get(i).activityInfo.packageName);
startActivity(mysharingIntent);

Upvotes: 1

Related Questions