Search code examples
androidbitmapassetsdecoder

Error on retrieve image from assets


I need to retrieve an image from the assets folder and I need to use a Bitmap to handle the image. The name of the file is image.jpg. This is the code

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AssetManager manager = getAssets();

        try {
            InputStream is = manager.open("image.jpg");
            Bitmap img = BitmapFactory.decodeStream(is);
            ImageView imageView = (ImageView) this.findViewById(R.id.ImageView1);
                imageView.setImageBitmap(img);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

But I can't understand why the app crashes. This is the error log.

06-28 18:17:16.488: E/AndroidRuntime(10567): FATAL EXCEPTION: main
06-28 18:17:16.488: E/AndroidRuntime(10567): Process: com.example.instacut, PID: 10567
06-28 18:17:16.488: E/AndroidRuntime(10567): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.instacut/com.example.instacut.MainActivity}: java.lang.NullPointerException
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.ActivityThread.access$800(ActivityThread.java:138)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.os.Looper.loop(Looper.java:136)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.ActivityThread.main(ActivityThread.java:5116)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at java.lang.reflect.Method.invokeNative(Native Method)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at java.lang.reflect.Method.invoke(Method.java:515)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at dalvik.system.NativeStart.main(Native Method)
06-28 18:17:16.488: E/AndroidRuntime(10567): Caused by: java.lang.NullPointerException
06-28 18:17:16.488: E/AndroidRuntime(10567):    at com.example.instacut.MainActivity.onCreate(MainActivity.java:41)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.Activity.performCreate(Activity.java:5231)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
06-28 18:17:16.488: E/AndroidRuntime(10567):    ... 12 more

I'm following a site on internet for this and it's exactly the same. I really don't understand what's wrong here. Thank you


Solution

  • You are receiving a Null Pointer error because imageView is actually a null object.

    First of all, there is an error in the code you posted:

       ImageView imageView = (ImageView);
       this.findViewById(R.id.ImageView1);
    

    Should be:

    ImageView imageView = (ImageView) this.findViewById(R.id.ImageView1);
    

    remove the first semicolon. If this wasn't the problem, then imageView is set to null because it's not properly associating with as resource within you're activity_main.xml file. Make sure that you have an ImageView declared with android:id="@+id/ImageView1" declared within activity_main.xml and not somewhere else. If that doesn't help, post the file and we will help you spot the error.

    In the future, if you ask an Android question, be sure to include any relevant xml/resource files in your post.