Android - 图库应用 NullPointerException - OnePlus One

Android - Gallery App NullPointerException - OnePlus One

我正在使用此代码通过我的应用程序打开图库应用程序 -

Intent galleryIntent = new Intent(Intent.ACTION_VIEW, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
startActivity(galleryIntent);

当我 select "Open with Gallery" 时,图库应用程序在 OnePlus One 设备上崩溃并显示此堆栈跟踪 -

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cyngn.gallerynext/com.cyngn.gallerynext.app.fullscreenviewer.FullscreenViewer}: java.lang.NullPointerException: uriString
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
                                                   at android.app.ActivityThread.access0(ActivityThread.java:154)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:135)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
                                                Caused by: java.lang.NullPointerException: uriString
                                                   at android.net.Uri$StringUri.<init>(Uri.java:470)
                                                   at android.net.Uri$StringUri.<init>(Uri.java:460)
                                                   at android.net.Uri.parse(Uri.java:432)
                                                   at com.cyngn.gallerynext.app.fullscreenviewer.FullscreenViewer.a(SourceFile:305)
                                                   at com.cyngn.gallerynext.app.fullscreenviewer.FullscreenViewer.onCreate(SourceFile:217)
                                                   at android.app.Activity.performCreate(Activity.java:5990)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
                                                   at android.app.ActivityThread.access0(ActivityThread.java:154) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:135) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

我不想将特定的 URI 传递给它,我只想让它打开默认的画廊文件夹。我该如何避免这种崩溃?

如果你想打开应用程序(因为你写:我只是想让它打开默认的图库文件夹):

Intent intent = new Intent("com.cyngn.gallerynext");

如果您想从图库中获取任何数据,see this post how to do it

不使用 setType("image/*")

Intent galleryIntent = new Intent(Intent.ACTION_VIEW, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivity(galleryIntent);

yourIntent.setType(String mimeType) 这是代表​​你想要获取的 MIME 类型数据。使用 android.provider.MediaStore.Images.Media 你有不变的 MIME。我想,在这里你可以找到更多 docs