简体   繁体   中英

Open Gallery App in Android

I am trying to open inbuilt gallery app pressing a button in my app.

I am trying out on Android 2.3 and above phones. The phones/tablet that I have are

Samsung S (Android 2.3.5) LG phone (Android 2.3.3) Nexus One (Android 2.3.6) Android Tablet (Android 4.0.3) Galaxy Nexus (Android 4.3)

I tried the following:

Intent intent = new Intent(Intent.ACTION_VIEW, null);
intent.setType("image/*");
startActivity(intent); 

above code works fine on Android tablet (4.0.3) and my Nexus phone too.. but if run the same app on any phone which is below 3.0 (gives me error)

08-24 11:47:53.628: E/AndroidRuntime(787):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
08-24 11:47:53.628: E/AndroidRuntime(787):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-24 11:47:53.628: E/AndroidRuntime(787):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-24 11:47:53.628: E/AndroidRuntime(787):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-24 11:47:53.628: E/AndroidRuntime(787):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 11:47:53.628: E/AndroidRuntime(787):  at android.os.Looper.loop(Looper.java:130)
08-24 11:47:53.628: E/AndroidRuntime(787):  at android.app.ActivityThread.main(ActivityThread.java:3687)
08-24 11:47:53.628: E/AndroidRuntime(787):  at java.lang.reflect.Method.invokeNative(Native Method)
08-24 11:47:53.628: E/AndroidRuntime(787):  at java.lang.reflect.Method.invoke(Method.java:507)
08-24 11:47:53.628: E/AndroidRuntime(787):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
08-24 11:47:53.628: E/AndroidRuntime(787):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-24 11:47:53.628: E/AndroidRuntime(787):  at dalvik.system.NativeStart.main(Native Method)
08-24 11:47:53.628: E/AndroidRuntime(787): Caused by: java.lang.NullPointerException
08-24 11:47:53.628: E/AndroidRuntime(787):  at com.cooliris.media.Gallery.onCreate(Gallery.java:323)
08-24 11:47:53.628: E/AndroidRuntime(787):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-24 11:47:53.628: E/AndroidRuntime(787):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-24 11:47:53.628: E/AndroidRuntime(787):  ... 11 more

So I tried the following:

Intent intent1= new Intent("android.intent.action.MAIN", null);
intent1.addCategory("android.intent.category.APP_GALLERY");
Intent intent2 = Intent.createChooser(intent1, "Gallery");
startActivity(intent2);

Again this works just fine with phones that are above/equalto 4.0 version. On 4.0 below phones it gives alert notification by saying:

"No application can perform this action"

Can somebody help me out with opening the Gallery from pressing a button from my app?

I figured out the way..

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
 "content://media/internal/images/media")); 
 startActivity(intent); 

This piece of code just opened the gallery without any issues. Could get it working on all versions!

Thought to put it as answer for people who are looking to open a Gallery on all versions.

Thanks Guys! :)

Try This out

btnGallery.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
    }
});

UPDATE onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_CANCELED) {
        if (requestCode == PICK_IMAGE) {
            Uri selectedImageUri = data.getData();
        } 
    }
}

UPDATE TO OPEN GALLERY APP

Intent galleryIntent = new Intent(Intent.ACTION_VIEW, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivity(galleryIntent);
Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(
                    Intent.createChooser(intent, "Complete action using"),
                    PICK_FROM_FILE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM