如何在SQLite数据库Android中存储图像或从中获取图像

时间:2019-06-29 09:41:37

标签: android bitmap imageview android-sqlite

我在一个片段中,并且有一个CircleImageView,默认情况下使用android:src="@drawable/default_profile_image"在xml中设置了一个图像。 用户可以决定是否加载其他图像,我可以通过启动一个意图来打开图库,然后在onActivityResult()中以这种方式加载图像:

if(requestCode==0 && resultCode == Activity.RESULT_OK) {
            Uri path = data.getData();
            Bitmap photo = null;
            try {
                photo = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), path);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(photo != null)
                mProfilePhoto.setImageBitmap(photo);
        }

现在,无论用户已上传另一张图像还是保留了默认图像,我都希望将其保存在数据库中,然后再使用它。 我已经有一种方法可以正确地从数据库中获取名称。

private void getUserInformation(){
        Cursor data = databaseHelper.getUserInformation();

        if(data.moveToFirst()) {
            //this crash 
            Bitmap bitmap = BitmapFactory.decodeByteArray(data.getBlob(2), 0, data.getBlob(2).length);
            if(bitmap != null)
                mProfilePhoto.setImageBitmap(bitmap);*/

            mProfileName = data.getString(1);

        }
        data.close();
    }

这是我暂时无法使用的内容

片段内的

onclick方法

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.saveProfileButton:
            //need to pass a bitmap insted of null
            addUserInformation(name,null);
            break;
        case R.id.profile_photo:
            Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            try {
                startActivityForResult(
                        Intent.createChooser(i, "Select Picture"), 0);
            }catch (ActivityNotFoundException ex){
                ex.printStackTrace();
            }
            break;
    }

}
private void addUserInformation(String username, Bitmap profile_pic){
    boolean b = databaseHelper.addUserInformation(username, profile_pic);
    if(b) {
        toastMessage("Update Success");
    }
    else
        toastMessage("Update Failed");
}

DatabaseHelper类

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "userinfo";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME1 = "USER_INFORMATION"; 

    public DatabaseHelper(Context context) {
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table "+TABLE_NAME1+" (ID INTEGER PRIMARY KEY AUTOINCREMENT, USERNAME TEXT, PICTURE BLOB)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME1);
        onCreate(db);
    }

    public boolean addUserInformation(String username, Bitmap picture){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("USERNAME", username);
        byte[] data = getBitmapAsByteArray(picture);
        values.put("PICTURE", data);
        long id = db.insert(TABLE_NAME1, null, values);
        db.close();
        if(id == -1)
            return false;
        else
            return true;

    }

    private static byte[] getBitmapAsByteArray(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
        return outputStream.toByteArray();
    }
}

1 个答案:

答案 0 :(得分:1)

修改您的方法:

private void getUserInformation(){
        Cursor data = databaseHelper.getUserInformation();

        if(data.moveToFirst()) {
            byte[] btyeArray = data.getBlob(2);
                if(btyeArray != null) {
                  Bitmap bitmap = BitmapFactory.decodeByteArray(btyeArray , 0, btyeArray .length);
                  if(bitmap != null) {
                      mProfilePhoto.setImageBitmap(bitmap);
                      photo = bitmap;
                    }else
                      mProfilePhoto.setImageResource(R.drawable.defaultImage); // set your default Image here
                 } else {
                   mProfilePhoto.setImageResource(R.drawable.defaultImage); // set your default Image here
                 }
            mProfileName = data.getString(1);

        }
        data.close();
    }
onActivityResult中的

位图photo将其定义为全局变量。并在调用addUserInformation时发送photo变量。

  case R.id.saveProfileButton:
            //need to pass a bitmap insted of null
            addUserInformation(name, photo);

修改您的DatabaseHelper方法

public boolean addUserInformation(String username, Bitmap picture){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("USERNAME", username);
        if(picture != null) {
          byte[] data = getBitmapAsByteArray(picture);
          values.put("PICTURE", data);
        }
        long id = db.insert(TABLE_NAME1, null, values);
        db.close();
        if(id == -1)
            return false;
        else
            return true;

    }
相关问题