Android:从SD卡显示图像

kmu*_*h79 15 android android-sdcard android-imageview

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

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);
Run Code Online (Sandbox Code Playgroud)

错误发生在最后一行(第28行,如下所示).

错误输出:

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
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助.

Luk*_*uth 33

我宁愿用a BitmapFactory来解码文件路径中的Image:

Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
jpgView.setImageBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)

文件说:

如果指定的文件名为null,或者无法解码为位图,则该函数返回null.

你可以检查代码是否适用于其他图像,以及是否可以在PC上打开图像.也许该文件已损坏.


rpt*_*r87 10

这段代码终于为我工作了:

    setContentView(R.layout.main);
    ImageView jpgView = (ImageView)findViewById(R.id.imageView);
    Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/sample-1.jpg");
    jpgView.setImageBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)

发生崩溃是因为在附加jpgview之前没有执行setContentView():

崩溃的代码:

    ImageView jpgView = (ImageView)findViewById(R.id.imageView);
    Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/sample-1.jpg");
    jpgView.setImageBitmap(bitmap);
    setContentView(R.layout.main);
Run Code Online (Sandbox Code Playgroud)


Lon*_*ren 7

String imagePath = Environment.getExternalStorageDirectory().toString() + PATH_TO_IMAGE;
return Drawable.createFromPath(imagePath)
Run Code Online (Sandbox Code Playgroud)