Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 502
TheDevMan Avatar asked Aug 24 '13 07:08

TheDevMan


1 Answers

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! :)

like image 103
TheDevMan Avatar answered Oct 11 '22 19:10

TheDevMan