1

BottomSheetDialogFragment was Called When i press the Add Button in My Main Activity , The BottomSheet Contain EditText and the ImageView ( The Image View from gridview )

The User enter the Edittext name and choose One Image in the Grid View and i want to Save the Edittext value and the User Selected Image into a New Table to create a Listview

I Dont Know How To Save the Selected Image in the SQLiteDatabase

Here is My Code

Database Helper

public class DatabaseHelper extends SQLiteOpenHelper {

   public static final String DATABASE_NAME = "slate.db";
   public static final String TABLE_NAME ="listview_name";
   public static final String COLUMN_ID="ID";
   public static final String COLUMN_TITLE="ITEM_NAME";
    private static final String COLUMN_IMAGE = "image_bitmap";;
    private  Context context;

    public DatabaseHelper(Context context)
   {
      super(context,DATABASE_NAME,null,1);
      this.context=context;

   }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

       String query = "CREATE TABLE " +TABLE_NAME +"("+COLUMN_ID + "INTEGER PRIMARY KEY AUTOINCREMENT,"+
               COLUMN_TITLE + "TEXT," +COLUMN_IMAGE +  " BLOB);";

       sqLiteDatabase.execSQL(query);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(sqLiteDatabase);

    }
    void createlist(String title, byte[] image)
    {
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues cv = new ContentValues();

        cv.put("COLUMN_TITLE", title);
        cv.put("COLUMN_IMAGE",   image);
       Long result =  sqLiteDatabase.insert( TABLE_NAME, null, cv );
        if (result==-1)
        {
            Toast.makeText(context, "Faild to create", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(context, "Created Sucessfully", Toast.LENGTH_SHORT).show();
        }
    }
}

BottomSheet

 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.new_list_create, container, false);

        ImageButton done = view.findViewById(R.id.done);
        final EditText listname = (EditText) view.findViewById(R.id.listname);
        final GridView gridView = (GridView) view.findViewById(R.id.gridview);

        final MyCustomAdpter customAdpter = new MyCustomAdpter(images, getContext());
        gridView.setAdapter(customAdpter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                customAdpter.selectedImage = i;
                customAdpter.notifyDataSetChanged();
                imageRes = images[i];
          

            }
        });




        done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DatabaseHelper myDB= new DatabaseHelper(getContext());
                myDB.createlist(listname.getText().toString(),);//how to get the selected image and Display here to add the item or How to Convert the blob into String

                String itemname = listname.getText().toString();
                if (!TextUtils.isEmpty(listname.getText().toString())) {

                    startActivity(new Intent(getContext(), CheckslateHome.class).putExtra("data", itemname).putExtra("image", imageRes));
                    dismiss();
                } else {
                    Toast.makeText(getContext(), "List Name not Empty ", Toast.LENGTH_SHORT).show();
                }

            }

        });


        return view;


    }
Karthickyuvan
  • 428
  • 2
  • 16

1 Answers1

0

probably looks like similar thread to this one !

https://stackoverflow.com/questions/9357668/how-to-store-image-in-sqlite-database#:~:text=Inorder%20to%20store%20images%20to,to%20set%20it%20to%20imageview.

Actually you can convert image(bitmap) to byteArray so we can store it as a blob inside sqlite table , please check out above like where answer is already marked , it has been mentioned both the function 1> How to convert image(bitmap) to blob or bytearray and 2> retrieve original image from it convert.

Nisarg Jani
  • 415
  • 2
  • 12