【问题标题】:Android : How To Insert GridView Selected Image into SqlLiteAndroid:如何将 GridView 选定的图像插入 Sqlite
【发布时间】:2020-08-23 17:10:14
【问题描述】:

当我在我的主活动中按下添加按钮时,BottomSheetDialogFragment 被调用,BottomSheet 包含 EditText 和 ImageView(来自 gridview 的图像视图)

用户输入 Edittext 名称并在网格视图中选择一个图像,我想将 Edittext 值和用户选择的图像保存到新表中以创建列表视图

我不知道如何将所选图像保存在 SQLiteDatabase 中

这是我的代码

数据库助手

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();
        }
    }
}

底页

 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;


    }

【问题讨论】:

    标签: android android-sqlite android-gridview


    【解决方案1】:

    可能看起来和这个类似!

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

    实际上你可以将图像(位图)转换为字节数组,这样我们就可以将它作为一个blob存储在sqlite表中,请查看上面已经标记答案的地方,它已经提到了两个函数1>如何转换图像( bitmap) 到 blob 或 bytearray 和 2> 从中检索原始图像转换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 2023-02-07
      • 2010-10-18
      • 2011-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多