Photopicker android

376 Views Asked by At

I am using retrofit Multipart request to upload an image and it is uplaoding successfully to the server, in another screen I want to implement the functionality to change or delete that image and when I fetch the image from the server I only got the name of the image and a Base URL by using which I can display the picture in ImageView/RecyclerView and the problem is I am not able to upload exact the same image again as I have only the name of the image, is there any way in android from where I can get the image as a File by just providing the name of the image like (1000007983.jpg).

I have tried creating the file from the URL which the server is returing but getting an error of file not found.

2

There are 2 best solutions below

0
On

if you can show image on ImageView then you can get it as Bitmap and then you can convert it back to File. This is the method I'm using to convert image to Bitmap and then Bitmap to File

 val bitmap = (binding.IDImg.drawable as BitmapDrawable).bitmap

  private fun bitmapToFile(bitmap: Bitmap): File? {
            val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
            val filesDir: File = applicationContext.filesDir
            val imageFile = File(filesDir, "temp_file_ $timeStamp.jpg")
            val os: OutputStream
            return try {
                os = FileOutputStream(imageFile)
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os)
                os.flush()
                os.close()
                imageFile
            } catch (e: java.lang.Exception) {
                Log.e(javaClass.simpleName, "Error writing bitmap", e)
                imageFile
            }
        }

After getting imageFile from that method I'm uploading again to server like this


 val imageFile = bitmapToFile(image)

                if (imageFile != null) {
                    val newImage = imageFile.asRequestBody("multipart/form-data".toMediaTypeOrNull())
                    body = MultipartBody.Part.createFormData("image", imageFile.name, newImage)
                    uploadID(type!!, body!!, name)
                }
0
On

No Sir you can't upload or change an image by its name what I will recommend is a little bit complicated approach but it is cleaner and probably less coding

First, you want to save the image on the server side as ByteArray,So in your database it should be saved as BLOB Type, And you can keep the same code for the upload Image Service API

Let us move to the client side save the image as a String on the client side and then when you receive the image from the server side it should save the ByteArray(BLOB in the database) in a string variable(client side) after that convert the string to byteArray in your android studio then you can get Bitmap object out of the byteArray, Finally use the bitmap with Library called Glide which can load bitmap to your imageView/recyclerView

this is the code for the client side when you receive the image from the back end (server-side) :

    val imageFromServer :String = *logic getting image byteArray from server*
    val bytes: ByteArray = Base64.decode(imageFromServer , Base64.DEFAULT)
    val imageInBitmap: Bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)

    //Glide Library
    Glide.with(context)
            .load(imageInBitmap)
            .centerCrop()
            .placeholder(*use your holder picture or don't*)
            .into(*your imageview i recommend to use binding view if you are not using it *)

and this is the dependency for Glide Library :

implementation 'com.github.bumptech.glide:glide:4.13.2'
annotationProcessor 'com.github.bumptech.glide:compiler:4.13.2'

From there you can change and delete the image as you desire and if you want to share it with other screens(Activities ) use SharedPrefrence instead of Intent.putExtra, Because Intent space's limit is 1MB and it won't handle the image's Bytes also will throw Exception and yes this answer is for dealing with MultipartFile please tell me if you still didn't understand or have a question about this approach and would be amazing if you provide me with some information about the back-end framework you are using, I answered by assuming that you are using Spring Boot but I can be more specific