15

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.

kmurph79
  • 1,192
  • 3
  • 13
  • 28

4 Answers4

33

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.

class Android
  • 762
  • 1
  • 6
  • 17
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • 1
    setImageBitmap(), but yeah, I had tried that before, and it gives me the exact same error. thanks. – kmurph79 Jul 17 '11 at 20:53
  • thanks again. I pushed a new image, and it gave me the same error. I should be getting an android phone soon to test with, I'm curious whether it will work there. This is so frustrating! – kmurph79 Jul 17 '11 at 23:23
  • What Android version runs on your Emulator? – Lukas Knuth Jul 18 '11 at 06:55
  • Sorry for the late reply, I'm running on 2.2 – kmurph79 Jul 20 '11 at 21:22
  • @LukasKnuth how can we access the image which is store in hidden folder within the SdCard? Example (.demo/abc.png) – Shubham Sejpal Aug 02 '18 at 09:03
  • @ShubhamSejpal pass the path into the `decodeFile()`-function. It shouldn't matter if the folders are hidden, as long as you have read-permission on the folder. – Lukas Knuth Aug 02 '18 at 11:57
10

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);
Puttaraju
  • 471
  • 7
  • 14
7
String imagePath = Environment.getExternalStorageDirectory().toString() + PATH_TO_IMAGE;
return Drawable.createFromPath(imagePath)
Londeren
  • 3,202
  • 25
  • 26
  • thanks. Environment.getExternalStorageDirectory() returns "/mnt/sdcard" so that helps. But it still gives me the same error at jpgView.setImageDrawable(drawable) – kmurph79 Jul 17 '11 at 21:11
3

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();
        }
Maveňツ
  • 1
  • 12
  • 50
  • 89
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
  • Please don't post the exact same answer to multiple questions. – George Stocker Oct 08 '12 at 14:15
  • ok i will! But i did it only, so that the user could find it easy, as a beginner i am learning too much here ... Thanks for advise – Pir Fahim Shah Oct 09 '12 at 15:59
  • 4
    I think there is no problem to post same answer to multiple questions in case that the answer does match the questions exactly. I think you are very kind and correct to post your answer here. Thank you @Pir Fahim Shah. And I think you can ignore the suggestion of George Stocker. – rml Nov 18 '13 at 07:02
  • 1
    @rml Thanks for appreciating me, yes i have the same idea, that as this forum is only about to get a solution of your problem so its not a jeopardy if i'll post the answer to many question if it match with the quality of the question then so. – Pir Fahim Shah Nov 18 '13 at 20:00
  • @PirFahimShah FYI `pls don't shout at` **us** – Maveňツ Jul 01 '15 at 08:41
  • 1
    @maveň LOL, yeah, actually in past i was been criticized for my precious work – Pir Fahim Shah Jul 02 '15 at 21:03