2

私のアプリでは、画像ビューでギャラリーから画像を設定しました。次に、その画像をSQLデータベースに保存します。

  1. SQLデータベースで何を変更する必要がありますか?(BLOB ??)
  2. 画像ビューから画像を取得してSQLデータベースに保存するための最良の方法は何ですか?

これが私のSQLデータベースです:

private static final String DATABASE_CREATE =
        "create table " + DATABASE_TABLE + " ("
                + KEY_ROWID + " integer primary key autoincrement, "
                + KEY_TITLE + " text not null, " 
                + KEY_BODY + " text not null, " 
                + KEY_DATE_TIME + " text not null);"; 

ここでは、ギャラリーで画像のパスを取得し、画像ビューで画像を設定します

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == PICK_FROM_FILE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);

            Log.v("IMAGE PATH====>>>> ",selectedImagePath);

         // Decode, scale and set the image.
            Bitmap myBitmap = BitmapFactory.decodeFile(selectedImagePath);
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, NEW_WIDTH, NEW_HEIGHT, true);
            myBitmap.recycle();
            myBitmap = null;

            mImageView.setImageBitmap(scaledBitmap);

        }
    }
}
4

3 に答える 3

3

first create table like this db.execSQL("create table if not exists tablename(....,images BLOB,.....);");

then do your java code like this,

  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  Bitmap bit = BitmapFactory.decodeFile("your image path here");
  bit.compress(CompressFormat.PNG, 0, outputStream);

  SQLiteDatabase db = dbh.getWritableDatabase();
  ContentValues cv = new ContentValues();
  ..
 cv.put("images", outputStream.toByteArray());
 ..
 ..
db.insert("tablename", null, cv);
db.close();

this will store your image to db and,

byte []temp = c.getBlob(3);
Bitmap image = BitmapFactory.decodeByteArray(temp, 0, temp.length);
BitmapDrawable dd = new BitmapDrawable(image.getImage());
imgView.setBackgroundDrawable(dd);

this will retrive image from db..

于 2012-06-04T09:59:43.750 に答える
2

または、画像を電話のメモリ(SDカード上)やアプリケーションキャッシュなどに保存し、画像ファイルを取得するためのパスをSQLデータベースに保存します。

于 2012-06-04T09:55:48.163 に答える
2

良い考えではありません。SQL操作を減らし、アプリケーションのパフォーマンスを向上させるために、ディスクにイメージを保存する必要があります。DBにイメージを保存すると、それぞれの意味でデータベースが重くなります。ディスクの目的の場所にイメージを保存して、データベーステーブルの文字列としての画像名と一緒のパス。

于 2012-06-04T10:11:12.317 に答える