简体   繁体   English

android中的位图图像问题

[英]issue with bitmap image in android

I use some functions to show some images in my android app. 我使用一些功能在我的android应用中显示一些图像。 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 尝试在空对象引用上调用虚拟方法'int android.graphics.Bitmap.getWidth()'

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! 我将其放在drawble文件夹中,并检查是否未从服务器加载图像,并且没有显示更多错误!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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