SoFunction
Updated on 2025-04-04

Android implements clicking on pictures to upload SQLite database

When using various apps, especially when posting on Moments and Weibo, you will choose to attach pictures, enter the mobile album, and choose the photos you want as part of the posting content. Here I will briefly introduce the method of clicking on the image to upload it.

1. Dynamic access to permissions

After Android 6.0, in addition to declaring permissions in the manifest file, what is usually done is to apply for permissions dynamically.

//Acquiring pictures from mobile phone album requires dynamic application permissionif((this,
       .READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
          (this,
                  new String[]{.READ_EXTERNAL_STORAGE},ALBUM_CODE);
                }else{
                    //If you have obtained permissions, then just take it                    takePhoto();
                }
//Authorization request callback
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case ALBUM_CODE:
                //Get photos                takePhoto();
                break;
        }
    }

2. Jump to the album and select photos

private void takePhoto() {
        Intent intent = new Intent(Intent.ACTION_PICK,null);
        (.EXTERNAL_CONTENT_URI,
                "image/*");
        startActivityForResult(intent,ALBUM_CODE);
    }

The implicit jump action involved is ACTION_PICK, which brings the image information back through the startActivityForResult callback with result.

3. Process image data and display it in ImageView.

//Callback returned from album    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        (requestCode, resultCode, data);

        switch (requestCode) {
            case ALBUM_CODE:
                Uri uri = ();
                if(uri != null){
//                                   Display the image in ImageView                    iv_goods.setVisibility();
                    //
                    (this).load(uri).into(iv_goods);
                }
                break;
        }

After displaying the image in the ImagView, if you want to save the image in the database, you need to do a series of conversions.

First of all, the data types supported by SQLite database include: int, String, text, and Blob, where Blob is the type used when storing multimedia such as pictures.

In fact, Blob can support multiple input types. Resources such as pictures usually need to be converted into byte streams and stored byte arrays. Therefore, in SQLite, byte arrays can also be used to store images, so Drawable-type images need to be converted into byte arrays.

// Convert Drawable object to byte array    public static byte[] getBytes(Drawable drawable){
        //Convert Drawable object to bitmap object        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        //Create an output byte stream        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        //compression        (, 1, bos);
        return ();
    }

I'm using Room components here

@ColumnInfo(name = "drawable")
    private byte[] drawable;

When obtaining image resources, get the byte array, convert it into Bitmap or Drawable, and you can take out the photo to display.

 //Convert byte array to Bitmap    public static Bitmap getBitmap(byte[] bytes){
        //BitmapFactory
        Bitmap bitmap = (bytes, 0, );
        return bitmap;
    }

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.