0

我想从我的内部存储中显示一张图片。

这是我的代码:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(getFilesDir()+"/screen.bmp"), "image/*");
startActivity(intent);

它不起作用。应用程序崩溃,logcat 上没有错误。我检查了该文件是否存在。

编辑 1:我认为我的 logcat 有问题。现在我有一个 NullPointerException。

05-26 21:09:13.560: E/AndroidRuntime(10163): FATAL EXCEPTION: main
05-26 21:09:13.560: E/AndroidRuntime(10163): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.gallery/com.android.camera.ViewImage}: java.lang.NullPointerException
05-26 21:09:13.560: E/AndroidRuntime(10163):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
05-26 21:09:13.560: E/AndroidRuntime(10163):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
05-26 21:09:13.560: E/AndroidRuntime(10163):    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
05-26 21:09:13.560: E/AndroidRuntime(10163):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
05-26 21:09:13.560: E/AndroidRuntime(10163):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 21:09:13.560: E/AndroidRuntime(10163):    at android.os.Looper.loop(Looper.java:130)
05-26 21:09:13.560: E/AndroidRuntime(10163):    at android.app.ActivityThread.main(ActivityThread.java:3835)
05-26 21:09:13.560: E/AndroidRuntime(10163):    at java.lang.reflect.Method.invokeNative(Native Method)
05-26 21:09:13.560: E/AndroidRuntime(10163):    at java.lang.reflect.Method.invoke(Method.java:507)
05-26 21:09:13.560: E/AndroidRuntime(10163):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
05-26 21:09:13.560: E/AndroidRuntime(10163):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
05-26 21:09:13.560: E/AndroidRuntime(10163):    at dalvik.system.NativeStart.main(Native Method)
05-26 21:09:13.560: E/AndroidRuntime(10163): Caused by: java.lang.NullPointerException
05-26 21:09:13.560: E/AndroidRuntime(10163):    at com.android.camera.MenuHelper.isWhiteListUri(MenuHelper.java:151)
05-26 21:09:13.560: E/AndroidRuntime(10163):    at com.android.camera.ViewImage.onCreate(ViewImage.java:617)
05-26 21:09:13.560: E/AndroidRuntime(10163):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-26 21:09:13.560: E/AndroidRuntime(10163):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
4

2 回答 2

0

检查这个,

// Launch default viewer for the file
Intent intent = new Intent();  
String path = getFilesDir().getAbsolutePath() + "/screen.bmp";                  
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(path),"image/*");
startActivity(intent);
于 2012-05-25T04:22:10.357 回答
0

我正在回答我自己的问题:

NullPointerException 的解决方案:

 Intent intent = new Intent();
 intent.setAction(Intent.ACTION_VIEW);
 Uri hacked_uri = Uri.parse("file://"+Environment.getExternalStorageDirectory()+"screen.bmp");
 intent.setDataAndType(hacked_uri, "image/*");
 startActivity(intent);

希望这对其他人有帮助!

于 2012-05-28T13:19:24.930 回答