Android:显示来自 SD 卡的图像 [英] Android: Display Image from SD CARD

查看:47
本文介绍了Android:显示来自 SD 卡的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我发疯!这是我的代码(我知道这个文件存在):

This is driving me insane! Here's my code (I know this file exists):

File imageFile = new File("/sdcard/gallery_photo_4.jpg");
ImageView jpgView = (ImageView)findViewById(R.id.imageView);
BitmapDrawable d = new BitmapDrawable(getResources(), imageFile.getAbsolutePath());
jpgView.setImageDrawable(d);

错误发生在最后一行(第 28 行,见下文).

The error occurs on that last line (line 28, referenced below).

错误输出:

W/dalvikvm(  865): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(  865): FATAL EXCEPTION: main
E/AndroidRuntime(  865): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.camera/org.example.camera.Imgview}: java.lang.NullPointerException
E/AndroidRuntime(  865):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(  865):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(  865):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(  865):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(  865):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  865):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  865):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  865):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  865):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  865):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  865):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  865):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  865): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  865):    at org.example.camera.Imgview.onCreate(Imgview.java:28)
E/AndroidRuntime(  865):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  865):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(  865):    ... 11 more
W/ActivityManager(   59):   Force finishing activity org.example.camera/.Imgview

我的布局看起来像(可能没有必要):

My layout looks like (probably not necessary):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/imageView" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:scaleType="center">
</ImageView>
</LinearLayout>

非常感谢您的帮助.

推荐答案

我宁愿使用 BitmapFactory 从文件路径解码图像:

I would rather use a BitmapFactory to decode the Image from the file-path:

Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
jpgView.setImageBitmap(bitmap);

文档说:

如果指定的文件名为空,或者无法解码为bitmap,函数返回null.

If the specified file name is null, or cannot be decoded into a bitmap, the function returns null.

您能否检查代码是否适用于另一个图像,以及您是否可以在 PC 上打开您的图像?可能文件已损坏.

Can you check if the code works with another image and if you can open your image on your PC thought. Maybe the file is corrupt.

这篇关于Android:显示来自 SD 卡的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆