안드로이드 개발 질문/답변
(글 수 45,052)
사진을 찍으면 이미지뷰에 보여지며 저장을 누르면 이미지뷰 사진이 db에 저장 되는 것을 구현 중입니다.
현재 사진을 찍어 이미지뷰에 출력까지는 하였고 그 이후 이미지를 byte로 변환 해서 db blob로 저장하는 것을 잘 모르겠습니다
텍스트 값은 저장 하겠는데 blob의 값을 저장 하는 방법을 모르겠네요..
이미지를 byte로 변환 하는 것까지 하긴 한것 같은데..맞는 지도 모르겠습니다
case R.id.regiet_save_btn:
if(mId != -1){
db = mDBHelper.getWritableDatabase();
db.execSQL("UPDATE item SET p1='"+ pp1.getText().toString()
+ "', p2='" + pp2.getText().toString()
+ "', p3='" + pp3.getText().toString()
+ "', p4='" + pp4.getText().toString()
+ "', p5='" + pp5.getText().toString()
+ "', p6='"+ map1.getText().toString()
+ "', p7='"+ map2.getText().toString()
+ "', p8='"+ img.get.toByteArray()
+ "' WHERE _id='"+mId+"';");
mDBHelper.close();
} else{
db = mDBHelper.getWritableDatabase();
db.execSQL("INSERT INTO item VALUES(null, '"
+ pp1.getText().toString() + "', '"
+ pp2.getText().toString() + "', '"
+ pp3.getText().toString() + "', '"
+ pp4.getText().toString() + "', '"
+ pp5.getText().toString() + "', '"
+ map1.getText().toString() + "', '"
+ map2.getText().toString() + "');");
mDBHelper.close();
}p8에 이미지를 어떤식으로 insert해야하는지..ㅠ.ㅠ
전체 소스는
package com.sung.item_regi;
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; import java.util.Locale;
import android.app.Activity; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Spinner; import android.widget.TextView;
public class item_regi_edit extends Activity implements OnClickListener, OnItemSelectedListener,LocationListener{
DBHelper mDBHelper;
TextView pp1,map1,map2;
EditText pp2,pp3,pp4,pp5;
ImageView img;
int mId;
String item;
ArrayAdapter<String> adapter;
LocationManager locManager;
Geocoder geoCoder;
Location myLocation = null;
double latPoint = 0;
double lngPoint = 0;
int TAKE_CAMERA = 1; // 카메라 리턴 코드값 설정
int TAKE_GALLERY = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.regi_edit);
pp1 = (TextView)findViewById(R.id.spintv);
pp2 = (EditText)findViewById(R.id.regiet_edit1);
pp3 = (EditText)findViewById(R.id.regiet_edit2);
pp4 = (EditText)findViewById(R.id.regiet_edit3);
pp5 = (EditText)findViewById(R.id.regiet_edit4);
map1 = (TextView)findViewById(R.id.map1);
map2 = (TextView)findViewById(R.id.map2);
img = (ImageView)findViewById(R.id.regiet_img1);
String[] arrayItems = {"","의류","악세사리","가전","맛집","뷰티","문구","기타"};
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, arrayItems);
Spinner spinner = (Spinner)findViewById(R.id.regiet_spin);
spinner.setPrompt("분류를 고르시오.");
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
setLocManager((LocationManager) getSystemService(Context.LOCATION_SERVICE));
geoCoder = new Geocoder(this, Locale.KOREAN);
Intent intent = getIntent();
mId = intent.getIntExtra("ParamID", -1);
item = intent.getStringExtra("ParamDate");
pp1.setText(intent.getStringExtra("Param1"));
pp2.setText(intent.getStringExtra("Param2"));
pp3.setText(intent.getStringExtra("Param3"));
pp4.setText(intent.getStringExtra("Param4"));
pp5.setText(intent.getStringExtra("Param5"));
map1.setText(intent.getStringExtra("Param6"));
map2.setText(intent.getStringExtra("Param7"));
mDBHelper = new DBHelper(this);
SQLiteDatabase db = mDBHelper.getWritableDatabase();
if(mId == -1){
pp1.setText(item);
} else{
Cursor cursor = db.rawQuery("SELECT * FROM item WHERE _id='" + mId + "'", null);
startManagingCursor(cursor);
if(cursor.moveToNext()){
pp1.setText(cursor.getString(1));
pp2.setText(cursor.getString(2));
pp3.setText(cursor.getString(3));
pp4.setText(cursor.getString(4));
pp5.setText(cursor.getString(5));
map1.setText(cursor.getString(6));
map2.setText(cursor.getString(7));
}
}
mDBHelper.close();
Button btn = (Button)findViewById(R.id.regiet_save_btn);
btn.setOnClickListener(this);
btn = (Button)findViewById(R.id.regiet_del_btn);
btn.setOnClickListener(this);
btn = (Button)findViewById(R.id.regiet_back_btn);
btn.setOnClickListener(this);
btn = (Button)findViewById(R.id.reset);
btn.setOnClickListener(this);
btn = (Button)findViewById(R.id.camera_btn);
btn.setOnClickListener(this);
btn = (Button)findViewById(R.id.album_btn);
btn.setOnClickListener(this);
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
pp1.setText(adapter.getItem(arg2));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
void GetLocations() {
// TODO Auto-generated method stub
TextView latText = (TextView) findViewById(R.id.map1);
TextView lngText = (TextView) findViewById(R.id.map2);
StringBuffer juso = new StringBuffer();
if (myLocation != null) {
latPoint = myLocation.getLatitude();
lngPoint = myLocation.getLongitude();
try {
List<Address> addresses;
addresses = geoCoder.getFromLocation(latPoint, lngPoint, 1);
for(Address addr: addresses){
int index = addr.getMaxAddressLineIndex();
for(int i=0;i<=index;i++){
juso.append(addr.getAddressLine(i));
juso.append(" ");
}
juso.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
latText.setText(String.valueOf(latPoint));
lngText.setText(String.valueOf(lngPoint));
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d("location", "location changed");
myLocation = location;
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
//카메라
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db;
switch(v.getId()){
case R.id.camera_btn:
Intent camera = new Intent();
camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera, TAKE_CAMERA);
break;
case R.id.album_btn:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, TAKE_GALLERY);
break;
case R.id.reset:
GetLocations();
Log.d("location", "button pressed");
break;
case R.id.regiet_save_btn:
if(mId != -1){
db = mDBHelper.getWritableDatabase();
db.execSQL("UPDATE item SET p1='"+ pp1.getText().toString()
+ "', p2='" + pp2.getText().toString()
+ "', p3='" + pp3.getText().toString()
+ "', p4='" + pp4.getText().toString()
+ "', p5='" + pp5.getText().toString()
+ "', p6='"+ map1.getText().toString()
+ "', p7='"+ map2.getText().toString()
+ "', p8='"+ img.get.toByteArray()
+ "' WHERE _id='"+mId+"';");
mDBHelper.close();
} else{
db = mDBHelper.getWritableDatabase();
db.execSQL("INSERT INTO item VALUES(null, '"
+ pp1.getText().toString() + "', '"
+ pp2.getText().toString() + "', '"
+ pp3.getText().toString() + "', '"
+ pp4.getText().toString() + "', '"
+ pp5.getText().toString() + "', '"
+ map1.getText().toString() + "', '"
+ map2.getText().toString() + "');");
mDBHelper.close();
}
Intent intent3 = new Intent(this,item_list.class);
startActivity(intent3);
startActivity(intent3);
break;
case R.id.regiet_del_btn:
if(mId != -1){
db = mDBHelper.getWritableDatabase();
db.execSQL("DELETE FROM item WHERE _id='"+mId+"';");
mDBHelper.close();
} else{
}
Intent intent1 = new Intent(this,item_list.class);
startActivity(intent1);
break;
case R.id.regiet_back_btn:
finish();
break;
}
}
public void onActivityResult( int $requestCode, int $resultCode, Intent $data ) {
if( $requestCode == TAKE_CAMERA ) {
Bitmap capturedImg = (Bitmap) $data.getExtras().get( "data" ) ;
ImageView imgView = (ImageView) findViewById( R.id.regiet_img1 ) ;
imgView.setImageBitmap( capturedImg ) ;
}
}
public byte[] bitmapToByteArray( Bitmap $bitmap ) {
ByteArrayOutputStream stream = new ByteArrayOutputStream() ;
$bitmap.compress( CompressFormat.JPEG, 100, stream) ;
byte[] byteArray = stream.toByteArray() ;
return byteArray ;
}
public void setLocManager(LocationManager locManager) {
this.locManager = locManager;
}
public LocationManager getLocManager() {
return locManager;
}
}
쉽게 이야기 해서 이미지뷰에 있는 이미지를 db에 저장 하고 싶습니다.




걍 디비는 일단 blob 형태로 테이블 세팅하신후
해당 이미지는 byte 배열형식으로 만든 후 넣어주시면됩니다.