0
드로어 블 폴더에있는 이미지를 SQlite 데이터베이스에 저장 한 다음 간단히 검색합니다. 나중에이 개념을 수정 하겠지만 ImageView에서는 아무 것도 얻지 못했습니다.SQLite 데이터베이스에 이미지 저장 및 검색
public class DBHelper extends SQLiteOpenHelper {
protected static final String DATABASE_NAME = "kamuskerul2";
private static final int DB_VERSION = 1;
private final Context myContext;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DB_VERSION);
this.myContext = context;
}
public void onCreate(SQLiteDatabase sqlitedatabase) {
sqlitedatabase.execSQL("CREATE TABLE tableimage (image BLOB);");
Bitmap mybitmap = BitmapFactory.decodeResource(myContext.getResources(), R.drawable.me);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
mybitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] img = bos.toByteArray();
ContentValues myContentValues = new ContentValues();
myContentValues.put("imag", img);
sqlitedatabase.execSQL("INSERT INTO tableimage (image) VALUES('"+myContentValues+"');");
}// END of onCreate().
public void onUpgrade(SQLiteDatabase sqlitedatabase, int i, int j) {
if (i >= j) {
return;
} else {
sqlitedatabase.execSQL("DROP DATABASE IF EXISTS kamuskerul2;");
onCreate(sqlitedatabase);
return;
}
}
}
그리고 다른 클래스 (이미지를 검색하여 표시) : 여기
는 코드입니다.private DBHelper dbhelper = new DBHelper(this);
private SQLiteDatabase db;
ImageView myTestedImageView;
byte[] imgimg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.MainActivity);
db = dbhelper.getReadableDatabase();
// ImageView
myTestedImageView = (ImageView)findViewById(R.id.myTestedImageViewXML);
}// end onCreate
public void onClick(View v) {
String image_name = "myContentValues";
Cursor cursor_image = db.rawQuery("SELECT * FROM tableimage " + "WHERE image='" + image_name + "';", null);
if (cursor_image.getCount() != 0) {
cursor_image.moveToFirst();
do {
imgimg = cursor_image.getBlob(cursor_image.getColumnIndex("image"));
} while (cursor_image.moveToNext());
Bitmap b1 = BitmapFactory.decodeByteArray(imgimg, 0, imgimg.length);
myTestedImageView.setImageBitmap(b1);
}
앱을 실행할 때 아무 것도 표시되지 않고 db도 비어 있습니다. 당신이 내 아이디어를 얻고이 문제를 극복 할 수 있기를 바랍니다.
내 목표를 달성하기 위해 단계별로 진행되는 튜토리얼을 알 수 있습니다. 아무튼 나는 안드로이드에 대해 알고 있고 더 많은 조언/검색이 필요합니다. TNX. – SDG69
내 대답의 링크는 다소 안드로이드 장치에 이미지를 저장하는 방법에 대한 자습서입니다. 해당 파일의 경로를 가져 와서 sqlite 데이터베이스에 문자열로 저장하십시오. – kevto