图像不会显示在ImageButton中

时间:2013-01-11 08:12:24

标签: android image bitmap inputstream outputstream

我正在显示字符串路径中的图像(我从数据库中检索),显示一个图像没有问题。但是当我有4个图像时,只显示一个图像(第一个图像)而其他图像不显示。我的代码如下:

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

}

//method to resize the bitmap before displaying
 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);

     return bitmap;
}

我在做什么有什么不对吗?请好好指出我的错误,因为我找不到我的错误。没有错误,没有,只是它没有显示。默认情况下,它只显示一个图像,它始终是第一个图像(psignature1),如果我将其注释掉,则会显示第二个图像(psignature2),但不会显示其余图像。

感谢您的时间!

2 个答案:

答案 0 :(得分:0)

//method to resize the bitmap before displaying
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();
    }
    // 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;
}

像这样编写你的方法......你就完成了。

答案 1 :(得分:0)

你的psignature1,psignature2,psignature3可能 由相同的id

定义

也许你的代码就像这样

psignature1 = (ImageButton) findViewById(R.id.psignature1);
psignature2 = (ImageButton) findViewById(R.id.psignature1);
psignature3 = (ImageButton) findViewById(R.id.psignature1);

尝试更改每个ID


这是sd卡中图像的位图压缩器, 只需编辑它

public Bitmap compressBitmapResource(String filePath, int reqWidth,
        int reqHeight) {

     // First decode with inJustDecodeBounds=true to check dimensions
     final BitmapFactory.Options options = new BitmapFactory.Options();

     options.inJustDecodeBounds = true;
     BitmapFactory.decodeFile(filePath, options);

     // Calculate inSampleSize
     options.inSampleSize = calculateBitmapSize(options, reqWidth,
     reqHeight);

     // Decode bitmap with inSampleSize set
     options.inPurgeable = true;
     options.inJustDecodeBounds = false;
     return BitmapFactory.decodeFile(filePath, options);

}

    public int calculateBitmapSize(BitmapFactory.Options options, int reqWidth,
        int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}