保存图像从相机中单击或从数据库中的库中加载并检索它

时间:2014-04-25 12:12:44

标签: android database blob store

我正在尝试将图像与用户输入的其他字段一起保存在imageview中。我希望将图像存储在数据库中,当要查看数据时,它将在其他页面上检索。我需要在“NewPat.java”以及“PatientInfoDB.java”中进行哪些更改。这是我的代码..

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;
        }

    }

数据库代码

    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

@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的页面我添加了这段代码

PatientInfoDB entry = new PatientInfoDB(this);
            entry.open();
            Bitmap bm=entry.getPhoto();

        iv.setImageBitmap(bm);

//在我的数据库中添加了此方法,即“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

2 个答案:

答案 0 :(得分:0)

这样做是为了从imageview获取位图:

Bitmap myBitmap= ((BitmapDrawable)image.getDrawable()).getBitmap();

然后你需要将位图转换为字节数组:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

然后你可以将它作为blob存储在DB

编辑:

检索bimap,从db获取字节数组并将其转换回位图

    Cursor c = db.raw......
    byte[] byteArray = c.getBlob(0);

    Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

现在您可以将ImageView位图设置为检索到的位图。

答案 1 :(得分:0)

由于您的使用时间是一次,因此您应该将图像存储在可以在整个项目中访问的公共静态变量中,而不是存储和检索数据库。

public static Uri publicSelectedImage; // Write this in NewPat at class level. 

onActivityResult()为此变量赋值,如下所示

Uri selectedImage = data.getData();
publicSelectedImage = selectedImage;         // write this line. 

现在,您可以通过以下方式访问此变量:

Uri publicImage = NewPat.publicSelectedImage;