samedi 27 février 2016

Saving Image SQLite Database

Hi I have this SQLite database which works fine, i have added a column called Image which has datatype Blob. I then allow the user to select an image from their gallery and from it I change it to a URi then to a byte[], but when i try and save the data a get no error messages but the data doesn't get saved, and I don't know why?

Any help?

My Query to create the table

 public String CREATEQUERY = "CREATE TABLE " + TableData.TableInfo.TABLE_NAME + "("+ TableData.TableInfo.USERNAME + " TEXT," + TableData.TableInfo.PASSWORD + " TEXT," + TableData.TableInfo.IMAGE + "BLOB);";

My Insert Method

public void InsertData (DatabasOpperations DBOpp, String Name, String Password, byte[] Image){
    SQLiteDatabase SQL = DBOpp.getWritableDatabase();
    ContentValues ContentV = new ContentValues();
    ContentV.put(TableData.TableInfo.USERNAME, Name);
    ContentV.put(TableData.TableInfo.PASSWORD, Password);
    ContentV.put(TableData.TableInfo.IMAGE, Image);
    long Insert = SQL.insert(TableData.TableInfo.TABLE_NAME, null, ContentV);
    Cursor Cur = SQL.query(TableData.TableInfo.TABLE_NAME, null, null, null, null, null, null);
    int Res = Cur.getCount();
    int Res2 = Cur.getColumnIndex(TableData.TableInfo.IMAGE);
    Log.d("DatabaseOperations", "Success, 1 Row Added");
    Log.d("DatabaseOperations", "Num " + Res);
    Log.d("DatabaseOperations", "Num " + Res2);

}

The java class which calls this method and saves the data

DB.InsertData(DB, Uname, Pass, DBByte);
Toast.makeText(getApplicationContext(), "Success, Registered!", Toast.LENGTH_SHORT).show();
Intent Home = new Intent(Reg.this, MainActivity.class);
startActivity(Home);
finish();

DBByte is a

public byte[] DBByte;

The Image convert from Uri to byte[]

public void onActivityResult(int RequestCode, int ResultCode, Intent Data) {
    if (ResultCode == RESULT_OK) {
        if (RequestCode == SELECTED) {
            Uri SelectedImageUri = Data.getData();
            SelectedImagePath = getPath(SelectedImageUri);
            Log.d("DatabaseOperations", "Image Path :  " + SelectedImagePath);
            Img.setImageURI(SelectedImageUri);

            try {
                FileInputStream FileInpStream = new FileInputStream(SelectedImagePath);
                BufferedInputStream BufInputStream = new BufferedInputStream(FileInpStream);
                DBByte = new byte[BufInputStream.available()];
                BufInputStream.read(DBByte);
                Log.d("DatabaseOperations", "Image Size :  " + DBByte.length + " KB");
            }

            catch (IOException e) {
                Log.d("DatabaseOperations", "Error :  " + SelectedImagePath);
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire