如何在eclipse中使用sql上传和检索图像

时间:2015-01-13 05:28:55

标签: java android sql eclipse

我希望有一个选项供用户上传两个图像并保存在数据库后检索图像,我是Android编程的新手,我正在尝试这个,因为四天,googled很多,但无法找到确切的解决方案。< / p>

private SQLiteStatement insertStmt;

private static final String INSERT = "insert into " + TABLE_NAME
        + " (name,number,skypeId,address) values (?,?,?,?)";

public DataManipulator(Context context) {
    DataManipulator.context = context;
    OpenHelper openHelper = new OpenHelper(DataManipulator.context);
    DataManipulator.db = openHelper.getWritableDatabase();
    this.insertStmt = DataManipulator.db.compileStatement(INSERT);

}

public long insert(String name, String number, String skypeId,
        String address) {
    this.insertStmt.bindString(1, name);
    this.insertStmt.bindString(2, number);
    this.insertStmt.bindString(3, skypeId);
    this.insertStmt.bindString(4, address);
    return this.insertStmt.executeInsert();
}

public void deleteAll() {
    db.delete(TABLE_NAME, null, null);
}

public List<String[]> selectAll() {

    List<String[]> list = new ArrayList<String[]>();
    Cursor cursor = db.query(TABLE_NAME, new String[] { "id", "name",
            "number", "skypeId", "address" }, null, null, null, null,
            "name asc");

    int x = 0;
    if (cursor.moveToFirst()) {
        do {
            String[] b1 = new String[] { cursor.getString(0),
                    cursor.getString(1), cursor.getString(2),
                    cursor.getString(3), cursor.getString(4) };

            list.add(b1);

            x = x + 1;
        } while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    cursor.close();

    return list;
}

public void delete(int rowId) {
    db.delete(TABLE_NAME, null, null);
}

private static class OpenHelper extends SQLiteOpenHelper {

    OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "
                + TABLE_NAME
                + " (id INTEGER PRIMARY KEY, name TEXT, number TEXT, skypeId TEXT, address TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

}

我的SaveData.java文件

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.save);
    View add = findViewById(R.id.Button01add);
    add.setOnClickListener(this);
    View home = findViewById(R.id.Button01home);
    home.setOnClickListener(this);
}

public void onClick(View v) {
    switch (v.getId()) {

    case R.id.Button01home:
        Intent i = new Intent(this, DatabaseSample.class);
        startActivity(i);
        break;

    case R.id.Button01add:
        View editText1 = (EditText) findViewById(R.id.name);
        View editText2 = (EditText) findViewById(R.id.number);
        View editText3 = (EditText) findViewById(R.id.skypeId);
        View editText4 = (EditText) findViewById(R.id.address);
        String myEditText1 = ((TextView) editText1).getText().toString();` `
        String myEditText2 = ((TextView) editText2).getText().toString();
        String myEditText3 = ((TextView) editText3).getText().toString();
        String myEditText4 = ((TextView) editText4).getText().toString();

        this.dh = new DataManipulator(this);
        this.dh.insert(myEditText1, myEditText2, myEditText3, myEditText4);

        showDialog(DIALOG_ID);
        break;

    }
}

protected final Dialog onCreateDialog(final int id) {
    Dialog dialog = null;
    switch (id) {
    case DIALOG_ID:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(
                "Information saved successfully ! Add Another Info?")
                .setCancelable(false)
                .setPositiveButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                SaveData.this.finish();

                            }
                        })
                .setNegativeButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });
        AlertDialog alert = builder.create();
        dialog = alert;
        break;

    default:

    }
    return dialog;
}

}

和CheckData.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.check);
    dm = new DataManipulator(this);
    names2 = dm.selectAll();

    stg1 = new String[names2.size()];

    int x = 0;
    String stg;

    for (String[] name : names2) {
        stg = name[1] + " - " + name[2] + " - " + name[3] + " - " + name[4];

        stg1[x] = stg;
        x++;
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, stg1);
    this.setListAdapter(adapter);
    selection = (TextView) findViewById(R.id.selection);

}

public void onListItemClick(ListView parent, View v, int position, long id) {
    selection.setText(stg1[position]);
}

}

1 个答案:

答案 0 :(得分:0)

要在数据库表中保存图像,您需要将其转换为字节数组,然后将其放在contentValue中。您可以创建一个保存图像的方法,如下所示 -

protected long saveBitmap(SQLiteDatabase database, Bitmap bmp)
{
    int size = bmp.getRowBytes() * bmp.getHeight(); 
    ByteBuffer b = ByteBuffer.allocate(size); bmp.copyPixelsToBuffer(b); 
    byte[] bytes = new byte[size];
    b.get(bytes, 0, bytes.length);
    ContentValues cv=new ContentValues();
    cv.put(CHUNK, bytes);
    this.id= database.insert(TABLE, null, cv);
}

要获取该记录,您可以这样做 -

byte[] bb = cursor.getBlob(cursor.getColumnIndex(/*Your column index for saving image*/));