如何显示从SQLite数据库到GridView的位图图像

时间:2018-09-07 15:22:05

标签: android image gridview bitmap

我创建了一个应用,该应用使用具有调整大小方法的自定义Gridview适配器来显示从可绘制对象到Gridview的图像

这是自定义的gridview适配器

public class PTSCustomGridView extends BaseAdapter{
private Context mContext;
private final String[] gridViewString;
private final int[] gridViewImageId;

public PTSCustomGridView(Context context, String[] gridViewString, int[] gridViewImageId) {
    mContext = context;
    this.gridViewImageId = gridViewImageId;
    this.gridViewString = gridViewString;
}

@Override
public int getCount() {
    return gridViewString.length;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(int i, View convertView, ViewGroup parent) {
    View gridViewAndroid;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {

        gridViewAndroid = new View(mContext);
        gridViewAndroid = inflater.inflate(R.layout.custom_gridview, null);
        TextView textViewAndroid = (TextView) gridViewAndroid.findViewById(R.id.android_gridview_text);
        ImageView imageViewAndroid = (ImageView) gridViewAndroid.findViewById(R.id.android_gridview_image);
        textViewAndroid.setText(gridViewString[i]);
        //imageViewAndroid.setImageResource(gridViewImageId[i]);
        imageViewAndroid.setImageBitmap(decodeSampledBitmapFromResource(mContext.getResources(), gridViewImageId[i], 50, 50));
    } else {
        gridViewAndroid = (View) convertView;
    }

    return gridViewAndroid;
}

public static int calculateInSampleSize(
        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) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) >= reqHeight
                && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                     int reqWidth, int reqHeight) {

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

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

}

这是用于生成图像到gridview的代码

        final String[] gridViewString = {
        "Excuse Me", "I'm Sorry", "Nice to Meet You", "What's Your Phone Number", "Can You Repeat that, Please?", "Thank You",

        };
        final int[] gridViewImageId = {
        R.drawable.excuseme, R.drawable.imsorry, R.drawable.nicemeet, R.drawable.phonenumber, R.drawable.repeatplease, R.drawable.thankyou,

        };

        PTSCustomGridView adapterViewAndroid = new PTSCustomGridView(PictureToSpeechActivity.this, gridViewString, gridViewImageId);

这是从数据库获取图像的代码

public void getImage(){
    int count = 0;
    Cursor c = db.rawQuery("SELECT ptsname, ptschoice FROM ptschoices", null);
    while(c.moveToNext()){
        /*byte[]image = c.getBlob(1);
        Bitmap bitmap = BitmapFactory.decodeByteArray(image,0, image.length);*/
        count++;
    }
    Toast.makeText(PictureToSpeechActivity.this, "Have Result "+count, Toast.LENGTH_LONG).show();
}

问题是我如何修改我的自定义适配器以接收位图而不是资源ID,并且考虑到我在自定义适配器上也有一种调整大小的方法,如何将其显示在gridview上

0 个答案:

没有答案