如何从我的sqlite数据库中显示多个图像

时间:2015-01-22 12:24:52

标签: android database sqlite

我有一个包含12张图像的图像数据库,我希望在我的活动中显示这些图像。这是我进行转换的代码:

public class Utility {
public static byte[] getBytes(Bitmap bitmap){
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
}

public static Bitmap getPhoto(byte[] image){
    return BitmapFactory.decodeByteArray(image, 0, image.length);
}}

这是我创建数据库条目的代码:

public long createSurveyImageRecord(byte[] sql_surv_img_1, byte[] sql_surv_img_2, byte[] sql_surv_img_3, byte[] sql_surv_img_4,
                                    byte[] sql_surv_img_5, byte[] sql_surv_img_6, byte[] sql_surv_img_7, byte[] sql_surv_img_8,
                                    byte[] sql_surv_img_9, byte[] sql_surv_img_10, byte[] sql_surv_img_11, byte[] sql_surv_img_12) {

    ContentValues cv = new ContentValues();

    cv.put(SURVIMG_1, sql_surv_img_1);
    cv.put(SURVIMG_2, sql_surv_img_2);
    cv.put(SURVIMG_3, sql_surv_img_3);
    cv.put(SURVIMG_4, sql_surv_img_4);
    cv.put(SURVIMG_5, sql_surv_img_5);
    cv.put(SURVIMG_6, sql_surv_img_6);
    cv.put(SURVIMG_7, sql_surv_img_7);
    cv.put(SURVIMG_8, sql_surv_img_8);
    cv.put(SURVIMG_9, sql_surv_img_9);
    cv.put(SURVIMG_10, sql_surv_img_10);
    cv.put(SURVIMG_11, sql_surv_img_11);
    cv.put(SURVIMG_12, sql_surv_img_12);

    return fieldInstDatabase.insert(DATABASE_TABLE_IMAGES, null, cv);
}

这是我从数据库中获取数据的代码:

public String getSurvImage() {

    String[] columns = new String[]{ROW_ID, SURVIMG_1, SURVIMG_2, SURVIMG_3, SURVIMG_4, SURVIMG_5, SURVIMG_6, SURVIMG_7, SURVIMG_8, SURVIMG_9, SURVIMG_10, SURVIMG_11, SURVIMG_12};

    Cursor c = fieldInstDatabase.query(DATABASE_TABLE_IMAGES, columns, null, null, null, null, null);

    String result = null;

    int iRow = c.getColumnIndex(ROW_ID);
    int iPic1 = c.getColumnIndex(SURVIMG_1);
    int iPic2 = c.getColumnIndex(SURVIMG_2);
    int iPic3 = c.getColumnIndex(SURVIMG_3);
    int iPic4 = c.getColumnIndex(SURVIMG_4);
    int iPic5 = c.getColumnIndex(SURVIMG_5);
    int iPic6 = c.getColumnIndex(SURVIMG_6);
    int iPic7 = c.getColumnIndex(SURVIMG_7);
    int iPic8 = c.getColumnIndex(SURVIMG_8);
    int iPic9 = c.getColumnIndex(SURVIMG_9);
    int iPic10 = c.getColumnIndex(SURVIMG_10);
    int iPic11 = c.getColumnIndex(SURVIMG_11);
    int iPic12 = c.getColumnIndex(SURVIMG_12);



    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){

        result = result + " " + c.getString(iRow) + " " + Utility.getPhoto(c.getBlob(iPic1)) +
                " " + Utility.getPhoto(c.getBlob(iPic2));



    }

    return result;
}

这是我在我的活动中显示的代码:

ImageView survImage = (ImageView) findViewById(R.id.ivSQLsurv);

    FieldInstallationDB survImg = new FieldInstallationDB(this);
    survImg.open();
    String pix = survImg.getSurvImage();
    survImg.close();

    byte[] px = pix.getBytes();


    survImage.setImageBitmap(Utility.getPhoto(px));

我的logcat消息:

Process: com.example.sweetiean.stlfieldinstallation1, PID: 14363
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sweetiean.stlfieldinstallation1/com.example.sweetiean.stlfieldinstallation1.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
        at android.app.ActivityThread.access$900(ActivityThread.java:175)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5602)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.sweetiean.stlfieldinstallation1.MainActivity.onCreate(MainActivity.java:170)



我也是这样尝试的:

 public Bitmap[] getSurvImage() {

    String[] columns = new String[]{ROW_ID, SURVIMG_1, SURVIMG_2, SURVIMG_3, SURVIMG_4, SURVIMG_5, SURVIMG_6, SURVIMG_7, SURVIMG_8, SURVIMG_9, SURVIMG_10, SURVIMG_11, SURVIMG_12};

    Cursor c = fieldInstDatabase.query(DATABASE_TABLE_IMAGES, columns, null, null, null, null, null);

    Bitmap[] result = null;

    int iRow = c.getColumnIndex(ROW_ID);
    int iPic1 = c.getColumnIndex(SURVIMG_1);
    int iPic2 = c.getColumnIndex(SURVIMG_2);
    int iPic3 = c.getColumnIndex(SURVIMG_3);
    int iPic4 = c.getColumnIndex(SURVIMG_4);
    int iPic5 = c.getColumnIndex(SURVIMG_5);
    int iPic6 = c.getColumnIndex(SURVIMG_6);
    int iPic7 = c.getColumnIndex(SURVIMG_7);
    int iPic8 = c.getColumnIndex(SURVIMG_8);
    int iPic9 = c.getColumnIndex(SURVIMG_9);
    int iPic10 = c.getColumnIndex(SURVIMG_10);
    int iPic11 = c.getColumnIndex(SURVIMG_11);
    int iPic12 = c.getColumnIndex(SURVIMG_12);



    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){

        //result = result + " " + c.getString(iRow) + " " + Utility.getPhoto(c.getBlob(iPic1));
        result = new Bitmap[] {Utility.getPhoto(c.getBlob(iPic1)), Utility.getPhoto(c.getBlob(iPic2))};



    }

    return result;
}

我的代码以活动显示:

ImageView survImage = (ImageView) findViewById(R.id.ivSQLsurv);

    FieldInstallationDB survImg = new FieldInstallationDB(this);
    survImg.open();
    Bitmap[] pix = survImg.getSurvImage();
    survImg.close();

    survImage.setImageBitmap(pix);

然后' pix'加下划线说" setImageBitmap不能应用于android.graphics.Bitmap []"

0 个答案:

没有答案