Java 在 Android 中打开图库应用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18416122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open Gallery App in Android
提问by TheDevMan
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
我正在尝试使用 Android 2.3 及更高版本的手机。我拥有的手机/平板电脑是
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)
三星 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)
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)
上面的代码在 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.access00(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:
这同样适用于高于/等于 4.0 版本的手机。在 4.0 以下的手机上,它会通过以下方式发出警报通知:
"No application can perform this action"
Can somebody help me out with opening the Gallery from pressing a button from my app?
有人可以帮助我通过按下我的应用程序中的按钮来打开图库吗?
采纳答案by TheDevMan
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! :)
谢谢你们!:)
回答by Vishal Vijay
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Complete action using"),
PICK_FROM_FILE);
回答by Biraj Zalavadia
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);