【问题标题】:One null image url prevents other things from getting displayed一个空图像 url 会阻止显示其他内容
【发布时间】:2013-01-14 07:18:34
【问题描述】:

我能够从 sqlite 数据库中获取数据。但是,当我的图像 url 为空时,它会阻止我的其他内容显示。

try{
                name.setText(nameString);
                school.setText(schoolString);
                psupervisor.setText(info.getPsupervisor());
                pdate.setText(info.getPdate());
                a.setChecked(Boolean.parseBoolean(info.getPtick1()));
                b.setChecked(Boolean.parseBoolean(info.getPtick2()));
                c.setChecked(Boolean.parseBoolean(info.getPtick3()));
                pcomment1.setText(info.getPcomment1());
                psignature1.setImageBitmap(resizeSignatureBitmap(info.getPsignature1()));
                pcomment2.setText(info.getPcomment2());
                psignature2.setImageBitmap(resizeSignatureBitmap(info.getPsignature2()));
                pdate2.setText(info.getPdate2());
                d.setChecked(Boolean.parseBoolean(info.getPtick4()));
                e.setChecked(Boolean.parseBoolean(info.getPtick5()));
                f.setChecked(Boolean.parseBoolean(info.getPtick6()));
                pcomment3.setText(info.getPcomment3());
                psignature3.setImageBitmap(resizeSignatureBitmap(info.getPsignature3()));
                pcomment4.setText(info.getPcomment4());
                psignature4.setImageBitmap(resizeSignatureBitmap(info.getPsignature4()));
                Log.d("PREPOST: ", log2);
                db.close();
            }
            catch(Exception ex){}

        }

例如,如果 psignature2 为 null,则不会显示从 pdate2 到 pcomment4 的项目。 没有因为 try-catch 而崩溃,因此我发现错误的原因晚了

我的位图分辨率调整方法:

public Bitmap resizeSignatureBitmap(String imagePath){

     BitmapFactory.Options options = new BitmapFactory.Options();
    InputStream is = null;
    try {
            is = new FileInputStream(imagePath);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    BitmapFactory.decodeStream(is,null,options);
    try {
            is.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    try {
            is = new FileInputStream(imagePath);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    // here w and h are the desired width and height
    options.inSampleSize = Math.max(options.outWidth/w, options.outHeight/h);
    // bitmap is the resized bitmap
    Bitmap bitmap = BitmapFactory.decodeStream(is,null,options);
    is=null;
     return bitmap;
 }

因此,任何人都可以建议我可以这样做的任何方法,即使有一个空图像 url,它也不会阻止其他图像的显示?谢谢!

【问题讨论】:

  • 你有一个大的 Try-Catch,如果出现问题代码停止运行,请尝试在单独的 try-catch 中设置图像。

标签: android sqlite android-widget bitmap


【解决方案1】:

resizeSignatureBitmap 中,您可以在末尾应用检查以检查要返回的bitmap 是否为null。如果是null,您可以返回一些默认位图。

或者您可以简单地在单独的try catch blocks 中设置图像。

【讨论】:

  • 另一种选择是在设置图像之前应用空检查。即只检查天气要应用的位图是否为空。如果不为null,则将其设置为ImageView,否则不要设置。
猜你喜欢
  • 2012-04-02
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多