halilkaya
halilkaya

Reputation: 467

Opening gallery as app programmatically

I want to open gallery when user presses a button. The code I use is this:

Intent resimGaleri = new Intent();
resimGaleri.setType("image/*");
resimGaleri.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Main.this.startActivity(resimGaleri);

However, in this code, when user presses the button to open gallery, Android asks 'Complete action using', but I want it to open gallery directly without asking. I can do that with the following code:

startActivity(new Intent("com.android.gallery3d"));

But I'm not sure if all devices use 'com.android.gallery3d' or not. Is it possible or is there any other way to do that?

Upvotes: 0

Views: 1880

Answers (2)

Aya Aboud
Aya Aboud

Reputation: 379

You could use this code: (it tested code not view massage ask you).

// select a file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);

Upvotes: 0

Robin
Robin

Reputation: 10368

Not all the devices have com.android.gallery3d, while most of them do. You can query the package manager by the intent action VIEW with MIME type image/* to get a list of activities. And then look through the list to find the correct one.

final Intent i = new Intent(Intent.ACTION_VIEW, null);
i.setType("image/*");
final List<ResolveInfo> apps = packageManager.queryIntentActivities(i, 0);
if(apps != null) {
    for(ResolveInfo info : apps) {
        if(info.resolvePackageName!=null && info.resolvePackageName.contains("gallery3d")) {//Maybe use more strict condition
             //This is the target you want
             //startActivity(XXXX);
             return;
        }
    }
    //Target not found
    //Start the first match or handle your exception here
} else {
//Handle exception
}

Upvotes: 2

Related Questions