Search code examples
androidsqliteormactiveandroid

How to store image using ActiveAndroid in DB


This My My Activity class code for Storing Values using activeandoid ... i have converted image file to byte in below code .. but it is not Stored in Db ...All other values were stored

case R.id.notVerified:
                    ActiveAndroid.beginTransaction();
                    try {
                        c.setName(Ename.getText().toString());
                        c.setEmail(Eemail.getText().toString());
                        c.setMemo(Eemail.getText().toString());
                        c.setPhone(Ephone.getText().toString());
                        c.setPhoto("user.png");
                        String s = new String(byteArray);

                        Toast.makeText(getActivity(), c.getImage().toString() ,Toast.LENGTH_LONG).show();
                        c.setImage(byteArray);
                        c.setTitle(ETitle.getText().toString());
                        c.save();
                        ActiveAndroid.setTransactionSuccessful();
                        Toast.makeText(getActivity(), "Contact Added",Toast.LENGTH_LONG).show();
                    } finally {
                        ActiveAndroid.endTransaction();
                    }

                    break;

This is for Converting Image to Byte

if (requestCode == CAMERA_REQUEST) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getActivity().getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            Eimgname.setText(filePath);
            cursor.close();

            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byteArray = stream.toByteArray();
            c.setImage(byteArray);
        }

Solution

  • ActiveAndroid also provides custom SQL queries using SQLiteUtils class. There are two methods executing SQL namely, “execSql” and “rawQuery”. The first one is provided for no results queries, whereas the second one for queries which return the result. In the Listing no. 12 you can find examples of uses of those methods.

    SQLiteUtils.execSql("DELETE FROM Prelegents where id = ?", new String[]{"1"});
    
    List prelegents =
        SQLiteUtils.rawQuery(PrelegentDAO.class, "SELECT * from Prelegents where id = ?", new String[]{"1"});
    

    SEE THIS LINK