转载

java – 在Android中打开Gallery App

我正在尝试按下我的应用程序中的按钮打开内置的图库应用程序.

我正在试用Android 2.3及以上的手机.我拥有的手机/平板电脑

三星S(Android 2.3.5)

LG手机(Android 2.3.3)

Nexus One(Android 2.3.6)

Android平板电脑(Android 4.0.3)

Galaxy Nexus(Android 4.3)

我尝试了以下方法:

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

上面的代码在Android平板电脑(4.0.3)和我的Nexus手机上运行良好..但如果在任何低于3.0的手机上运行相同的应用程序(给我错误)

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

所以我尝试了以下内容:

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

再次,这适用于高于/等于4.0版本的手机.在4.0以下的手机上,它通过说:

"No application can perform this action"

有人可以帮助我从我的应用程序按下按钮打开图库吗?

我想通了……

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

这条代码刚刚打开了画廊,没有任何问题.可以让它适用于所有版本!

想把它作为想要在所有版本上打开图库的人的答案.

多谢你们!

翻译自:https://stackoverflow.com/questions/18416122/open-gallery-app-in-android

原文  https://codeday.me/bug/20190113/514735.html
正文到此结束
Loading...