如何从imageView中获取Android数据库中的图像

时间:2016-07-23 10:57:52

标签: android

首先,我是android的初学者。如果我犯了一些错误,那就很抱歉。

所以现在我想将图像存储在android数据库中,该图像从图像视图中获取该图像。图像将由相机拍摄的ImageView保持。 数据库类是:

public class DatabaseOperation extends SQLiteOpenHelper {
    SQLiteDatabase db;

    public static final String DATABASE_NAME = "Student.db";
    public static final String TABLE_NAME = "student_table";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "PASS";
    public static final String COL_4 = "CONTACT";
    public static final String COL_5 = "NIC";
    public static final String COL_6= "CONFIRM";
      public static final String IMAGE_KEY="IMAGE";

    public DatabaseOperation (Context context) {

        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PASS TEXT,CONFIRM TEXT,NIC INTEGER,CONTACT INTEGER,IMAGE_KEY BLOB)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }
    public  boolean insertData(String name,String pass,String confrim,String contact,String nic,byte[]image)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(COL_2,name);
        cv.put(COL_3,pass);
        cv.put(COL_6,confrim);
        cv.put(COL_4,contact);
        cv.put(COL_5,nic);
        cv.put(IMAGE_KEY,image);
       long result= db.insert(TABLE_NAME,null,cv);
        if(result==-1)
            return false;
        else
            return true;

        }
    }
}

现在我想通过点击注册按钮存储图像视图中的图像。

enter image description here

在此屏幕截图中,此图像是锁定在ImageView我想通过单击注册按钮将此图像存储在数据库中

这是活动类 现在我不知道如何在活动课中使用它:

public void reg(View v) {

    if(!(pass.getText().toString().equals(confirm.getText().toString()))) {
        showMessage("Error!","Password not matched");
        pass.setText("");
        confirm.setText("");
    } else {
        boolean isInserted = myDb.insertData(name.getText().toString(), pass.getText().toString(),
        confirm.getText().toString(), cont.getText().toString(), nic.getText().toString());

        if (isInserted == true)
            Toast.makeText(getBaseContext(), "Registration Succes!", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(getBaseContext(), "No Record     Registered!", Toast.LENGTH_SHORT).show();

    }
}

2 个答案:

答案 0 :(得分:0)

您可以将图像存储在类型为BLOB的sqlite数据库中,这是示例数据库代码

public class DatabaseHelper extends SQLiteOpenHelper {
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "database_name";

    // Table Names
    private static final String DB_TABLE = "table_image";

    // column names
    private static final String KEY_NAME = "image_name";
    private static final String KEY_IMAGE = "image_data";

    // Table create statement
    private static final String CREATE_TABLE_IMAGE = "CREATE TABLE " + DB_TABLE + "("+ 
                       KEY_NAME + " TEXT," + 
                       KEY_IMAGE + " BLOB);";

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

    @Override
    public void onCreate(SQLiteDatabase db) {

        // creating table
        db.execSQL(CREATE_TABLE_IMAGE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // on upgrade drop older tables
        db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);

        // create new table
        onCreate(db);
    }
}

在数据库中插入:

public void addEntry( String name, byte[] image) throws SQLiteException{
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues cv = new  ContentValues();
    cv.put(KEY_NAME,    name);
    cv.put(KEY_IMAGE,   image);
    database.insert( DB_TABLE, null, cv );
}

检索数据:

byte[] image = cursor.getBlob(1); 

在将位图插入db

之前,必须将位图转换为byte []

答案 1 :(得分:0)

您需要将图像转换为字节数组,然后将其传递到insertData方法中。您可以从imageview(假设您想要保存锁定图标)或直接从您的资源获取图像。

试试这个,

    // get image from ImageView
    // imageView is your your ImageView
    Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    int bytes = bitmap.getByteCount();
    //Create a new buffer
    ByteBuffer buffer = ByteBuffer.allocate(bytes);
    //Move the byte data to the buffer
    bitmap.copyPixelsToBuffer(buffer);

    // Image as byte array
    byte[] array = buffer.array();

    // your insert code
    boolean isInserted = myDb.insertData(
        name.getText().toString(), 
        pass.getText().toString(),
        confirm.getText().toString(), 
        cont.getText().toString(), 
        nic.getText().toString(),
        array
    );