0

我正在显示来自字符串路径(我从数据库中检索)的图像,并且显示一张图像没有问题。但是当我有 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),但不会显示其余图像。

感谢您的时间!

4

2 回答 2

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

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

于 2013-01-11T08:22:29.120 回答
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;
}
于 2013-01-11T08:29:28.733 回答