Android:显示 SD 卡中的图像

发布于 2024-11-24 18:15:25 字数 2683 浏览 6 评论 0原文

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

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 行,下面引用)。

错误输出:

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

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

<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>

非常感谢您的帮助。

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);

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

Error output:

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>

Thank you very much for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

○闲身 2024-12-01 18:15:25

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

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

文档说:

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

你能检查一下代码是否适用于其他图像以及是否可以在你的电脑上打开你的图像。也许该文件已损坏。

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

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

The Docs say:

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

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.

不必了 2024-12-01 18:15:25

这段代码最终对我有用:

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

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

崩溃的代码:

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

This code worked for me finally:

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

Crash was happening because setContentView() was not performed before attaching the jpgview:

code that was crashing:

    ImageView jpgView = (ImageView)findViewById(R.id.imageView);
    Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/sample-1.jpg");
    jpgView.setImageBitmap(bitmap);
    setContentView(R.layout.main);
晌融 2024-12-01 18:15:25
String imagePath = Environment.getExternalStorageDirectory().toString() + PATH_TO_IMAGE;
return Drawable.createFromPath(imagePath)
String imagePath = Environment.getExternalStorageDirectory().toString() + PATH_TO_IMAGE;
return Drawable.createFromPath(imagePath)
未央 2024-12-01 18:15:25

使用这行代码从 SDCARD 获取图像。然后将其显示在您的 IMAGEVIEW 中

,其中“FileInputOutput”是您 sdcard 中的文件夹

    String path = Environment.getExternalStorageDirectory()+ "/FileInputOutput/img1.jpg"; 
            File imgFile = new File(path);
            if(imgFile.exists())
        {
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());                  
                ImageView myImage = (ImageView) findViewById(R.id.imageView1);
                myImage.setImageBitmap(myBitmap);
        }
            else                    
                Toast.makeText(v.getContext(),"no IMAGE IS PRESENT'",Toast.LENGTH_SHORT).show();
        }

USE THIS LINE OF CODE FOR GETTING IMAGE FROM SDCARD. AND THEN DISPLAY IT IN YOUR IMAGEVIEW

where "FileInputOutput" is a folder in your sdcard

    String path = Environment.getExternalStorageDirectory()+ "/FileInputOutput/img1.jpg"; 
            File imgFile = new File(path);
            if(imgFile.exists())
        {
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());                  
                ImageView myImage = (ImageView) findViewById(R.id.imageView1);
                myImage.setImageBitmap(myBitmap);
        }
            else                    
                Toast.makeText(v.getContext(),"no IMAGE IS PRESENT'",Toast.LENGTH_SHORT).show();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文