简体   繁体   中英

issue with bitmap image in android

I use some functions to show some images in my android app. Images read from server and all this works good. But I don't know why, my app show force close and the error is:

Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference

This is code that error is from there:

  Bitmap source = BitmapFactory.decodeFile(path);
    int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;

finally i used and default image for my problem. i put it in drawble folder and check if images not loaded from server it shows and no more error shows up!

UPDATE:

here is how i use this method:

Bitmap source = BitmapFactory.decodeFile(path);
    if (source == null){
        source = BitmapFactory.decodeResource(MainActivity.getContext().getResources(), R.drawable.no_pic_feed);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM