1

I get an image from an user which is converted into a bitmap. I then convert the bitmap to a an byte array and send it over JSON to be stored in a database. Then when the user starts the particular activity I want the image to be retrieved form the database and displayed to the user.

In the application a user is able to make a post with a title, description and an image. I want these three variables stored in the database so that when someone else views the post, they are able to see all the content. Also the image would be stored in the database as a blob, i simply use JSON to send the data to an backend application which handles all the communication with the DB.

My problem is that the bitmap that I get seems to be a reference to some memory on the device android.graphics.Bitmap@324a72b which changes every time I run the application although I select the same image. I want to be able to retrieve the actual bitmap so it can be stored in a DB. I'm also not using as web server for storing the images since its a smaller project.

b.buttonNewItemUpImg.setOnClickListener {
            val openGalleryIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
            startActivityForResult(openGalleryIntent, ResultLoadImage)
        }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == ResultLoadImage){
            if (resultCode == Activity.RESULT_OK){
                var temp = MediaStore.Images.Media.getBitmap(this.contentResolver, data!!.getData())
                bitmap = getResizedBitmap(temp!!, maxImageSize)
                b.imageView.setImageURI(data!!.getData())
            }
        }
    }

The getResizedBitmap() function simply makes the image smaller.

Nevar
  • 11
  • 2
  • `get an image from an user which is converted into a bitmap. I then convert the bitmap to a an byte array` I think you get a jpg file from a user. If you make a bitmap out of it and then compress it to a byte array then the byte array contains the bytes of a jpg file. So why all these unnecessary actions? You also dont need json to put the array in a database but could store it as blob. – blackapps Nov 21 '21 at 17:36
  • `Getting picture from user ....` Ahum... No user is sending you a picture. You let the user of your ap pick an image with ACTION_PICK. Quite confusing title. – blackapps Nov 21 '21 at 17:41
  • Sorry for the explanation. I now realize it wasn't sufficient. In the application a user is able to make a post with a title, description and an image. I want these three variables stored in the database so that when someone else views the post, they are able to see all the content. Also the image would be stored in the database as a blob, i simply use JSON to send the data to an backend application which handles all the communication with the DB. – Nevar Nov 21 '21 at 18:53
  • Now if you place the text of that comment at the start of your post... – blackapps Nov 21 '21 at 19:54

2 Answers2

0

As described in https://stackoverflow.com/a/57576381/6359367

Use this

      var temp = ImageDecoder.createSource(this.getContentResolver(), data!!.getData())
      bitmap = ImageDecoder.decodeBitmap(temp);

beware this code was java I tried to adapt it to kotlin you may need to tweak it a bit

shahawi273
  • 25
  • 9
0

Instead of storing the whole image in the database, you should copy images to the app's local storage and then store, path of these copied images to the database, which you can use later to retrieve the images.

Copying image to the app's local storage after getting the selected image uri

//creating an image file in the app's local storage
val dir = requireActivity().getDir(FOLDER_TO_SAVE, Context.MODE_PRIVATE).path
val copiedImageFile = File(dir, UUID.randomUUID().toString() + ".jpg")
val copiedImageUri = Uri.fromFile(copiedImageFile)

//copying selected image to the local storage
data!!.getData().path?.let {
    File(it).copyTo(copiedImageFile)
}

Get path of the above copied image, using its uri

copiedImageUri.path.?.let{
     val copiedImagePath = "file://$it"
     //now save this path to your database
}

Use this saved image path to retrieve the image in any fragment or activity.

Praveen
  • 3,186
  • 2
  • 8
  • 23
  • This would work but the image needs to be stored in the database so other users can have access and see the image. – Nevar Nov 21 '21 at 18:58
  • What do you mean by other users? App's database and local storage are only accessible by the app only. – Praveen Nov 22 '21 at 06:54
  • I don't mean the apps database. I have an external database where I want these images to be stored, so that I can retrieve them whenever a different user starts an activity where these images will be displayed. – Nevar Nov 23 '21 at 14:10