问题描述
我想在图像中的ImageView保存非常久远的其他领域的用户输入。我希望存储在数据库中的图像和将其它页面上时,数据是要观看检索。什么样的变化,我需要我的NewPat.java,以及PatientInfoDB.java进行。这里是我的code ..
I am trying to save the image in imageview alongwith the other fields that user inputs. I wish to store the image in database and it will be retrieved on other page when the data is to be viewed. What changes do i need to make in my "NewPat.java" as well as "PatientInfoDB.java". Here's my code..
public class NewPat extends Activity implements OnClickListener{
//Display pic
ImageView iv;
Intent i;
final static int cameraData=0;
Bitmap bmp;
//from gallery
Button buttonLoadImage;
private static int RESULT_LOAD_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.newpat);
Initialize();
setCurrentDate();
addButtonListener();
//Display pic
InputStream is=getResources().openRawResource(R.drawable.ic_launcher);
bmp=BitmapFactory.decodeStream(is);
}
private void Initialize() {
//Display pic
ib=(Button)findViewById(R.id.ibTakePic);
iv=(ImageView)findViewById(R.id.ivReturnedPic);
buttonLoadImage = (Button) findViewById(R.id.bLoadPicture);
ib.setOnClickListener(this);
buttonLoadImage.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bsave:
//Collecting data from edittexts
// getting Name
first = fname.getText().toString();
middle = mname.getText().toString();
last = lname.getText().toString();
//getting the id of selected radio button
int genderId = radiogrp.getCheckedRadioButtonId();
genderButton = (RadioButton) findViewById(genderId);
gender = genderButton.getText().toString();
//Getting DOB
date = dob.getText().toString();
//getting age
age= setage.getText().toString();
//getting admission date
admm =admitdate.getText().toString();
//getting address
addr = address.getText().toString();
//getting email
email=emailid.getText().toString();
//getting phone
phone = contact.getText().toString();
// How do i collect the image from imageview????
//Inserting in PatientinfoDB
PatientInfoDB entry = new PatientInfoDB(NewPat.this);
entry.open();
// here i need to pass the image along with other parameters in database
entry.createEntry(first,middle,last,gender,date,age,admm,addr,email,phone);
entry.close();
break;
//camera case
case R.id.ibTakePic:
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);//zero can be replaced with any action code
break;
case R.id.bLoadPicture:
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);//one can be replaced with any action code
break;
}
}
数据库code
Database code
public class PatientInfoDB {
public static final String KEY_ROWID = "_id";
public static final String KEY_FNAME = "pat_fname";
public static final String KEY_MNAME = "pat_mname";
public static final String KEY_LNAME="pat_lname";
public static final String KEY_GENDER="pat_gender";
public static final String KEY_DOB="pat_dob";
public static final String KEY_AGE="pat_age";
public static final String KEY_ADMISSION="pat_admission";
public static final String KEY_ADDRESS="pat_address";
public static final String KEY_CONTACT="pat_phone";
public static final String KEY_EMAILID="pat_email";
public static final String KEY_PHOTO = "pic_dp";
private static final String DATABASE_NAME = "PatientdataDB";
private static final String DATABASE_TABLE = "PersonalDetails";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_FNAME + " TEXT NOT NULL, " +
KEY_MNAME + " TEXT NOT NULL, " +
KEY_LNAME + " TEXT NOT NULL, " +
KEY_GENDER + " TEXT NOT NULL, " +
KEY_DOB + " TEXT NOT NULL, " +
KEY_AGE + " TEXT NOT NULL, " +
KEY_ADMISSION + " TEXT NOT NULL, " +
KEY_ADDRESS + " TEXT NOT NULL, " +
KEY_EMAILID + " TEXT NOT NULL, " +
KEY_CONTACT + " TEXT NOT NULL, " +
KEY_PHOTO + " BLOB);"
);
}
public long createEntry(String first, String middle, String last,String gender, String date, String age, String admm, String addr, String email, String phone, byte[] image) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_FNAME, first);
cv.put(KEY_MNAME, middle);
cv.put(KEY_LNAME, last);
cv.put(KEY_GENDER, gender);
cv.put(KEY_DOB, date);
cv.put(KEY_AGE, age);
cv.put(KEY_ADMISSION, admm);
cv.put(KEY_ADDRESS, addr);
cv.put(KEY_CONTACT, phone);
cv.put(KEY_EMAILID, email);
cv.put(KEY_PHOTO, image);
//startobv
//vc.put(KEY_DATETIME, value)
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
的onActivityResult
OnActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
Uri selectedImage = data.getData();
iv.setImageURI(selectedImage);
}
break;
case 1:
if(resultCode == RESULT_OK){
Uri selectedImage = data.getData();
iv.setImageURI(selectedImage);
}
break;
}
}
//页面,PIC提供显示。B我加入这片code的
//Page where the pic has to b displayed i added this piece of code
PatientInfoDB entry = new PatientInfoDB(this);
entry.open();
Bitmap bm=entry.getPhoto();
iv.setImageBitmap(bm);
//添加这个方法在我的数据库,即PatientInfoDB.java
// Added this method in my database i.e. "PatientInfoDB.java"
public Bitmap getPhoto(){
Cursor c = ourDatabase.rawQuery("SELECT pic_dp FROM PersonalDetails ;", null);
byte[] byteArray = c.getBlob(0);
Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
return bm;
}
但我得到了错误android.database.CursorIndexOutOfBoundException:指数-1要求,大小为2
But i got the error "android.database.CursorIndexOutOfBoundException:Index -1 requested,with a size of 2
推荐答案
这样做是为了获得从ImageView的位图:
Do this to get the bitmap from the imageview:
Bitmap myBitmap= ((BitmapDrawable)image.getDrawable()).getBitmap();
,那么你需要将位图转换为字节数组:
then you need to convert the bitmap to a byte array:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
然后就可以将其存储在数据库中的BLOB
then you can store it as a blob in the DB
编辑:
检索您从DB字节数组的bimap的,并将其转换回位图
to retrieve the bimap you get the byte array from the db and convert it back to a bitmap
Cursor c = db.raw......
byte[] byteArray = c.getBlob(0);
Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
现在你可以设置的ImageView
位图检索位图。
Now you can just set the ImageView
bitmap to the retrieved bitmap.
这篇关于保存图像从相机点击或数据库从画廊加载和检索它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!