从内部存储器在屏幕上显示图片

时间:2012-05-24 23:44:37

标签: android android-intent image

我想显示内部存储空间中的图片。

这是我的代码:

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)

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);

希望这有助于他人!