0
votes

I have stored the bitmap from the camera capture into the photo variable. Now I want to convert it to URI so that I can store it in a file and then store it onto my server.

Here's how I store the bitmap image:

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && data !=null) {
            if (requestCode == REQUEST_IMAGE_CAPTURE){
                val imageView = view!!.findViewById<ImageView>(R.id.display_image)

                //FOR CAMERA CAPTURE
                                 photo = data.getExtras().get("data") as Bitmap
//                getImageUriFromBitmap(context,photo!!)

                 filePath = getImageFilePath(data)
                val outputFileUri1 =getImageUriFromBitmap(activity!!,photo!!)
                intermediate = activity!!.contentResolver.getType(outputFileUri)

                if (filePath != null) {

                        mBitmap = BitmapFactory.decodeFile(filePath)
                        imageView.setImageBitmap(mBitmap)

                }
                                imageView.setImageBitmap(photo)

//                 finalPath = photo.toString()

//                finalPath = getImageUri(context,photo)

//                Log.d("getting camera path",finalPath)
//                val scaled = Bitmap.createScaledBitmap(photo,500,500,true)
//                val photo = Bitmap.createScaledBitmap(data.getExtras().get("data") as Bitmap,50,50,true)

//                var cameraconverted = getImageUri(getActivity()!!.getApplicationContext(),photo!!)
//                 finalPathOfCamera = getPathFromURI(cameraconverted)
//
//                Log.d("finalPathOfCamera",finalPathOfCamera)


            } else if(requestCode== IMAGE_PICK_CODE) {

                val imageView = view!!.findViewById<ImageView>(R.id.display_image)

                // Get image URI From intent FOR UPLOADING

                                 imageUri = data.data

                intermediate = activity!!.contentResolver.getType(imageUri!!)

                filePath = getImageFilePath(data)
                if (filePath != null) {

                    mBitmap = BitmapFactory.decodeFile(filePath)
                    imageView.setImageBitmap(mBitmap)

                    cursor!!.close()
                }

                makeComplaint_button!!.backgroundTintList = activity!!.getColorStateList( R.color.color_selector)
                makeComplaint_button!!.isEnabled = true
//                // do something with the image URI
//                imageView.setImageURI(imageUri)

//                var finalPathOfUpload = imageUri!!.path
//                 finalPathOfUpload = getPathFromURI(imageUri!!)



//                 finalPath = imageUri.toString()

//                Log.d("getting upload path",finalPath)
//                Log.d("getfinalPathOfUpload",finalPathOfUpload)




            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }

Right now I'm using this: photo = data.getExtras().get("data") as Bitmap to store my image and display it using this : imageView.setImageBitmap(photo) It works fine.

But I want to use this function : filePath = getImageFilePath(data) to get the URI and store it as URI in ouputFileUri and then as a file in filePath.

This is how getImageFilePath works:

   fun getImageFilePath(data: Intent): String {
        return getImageFromFilePath(data)
    }

    private fun getImageFromFilePath(data: Intent?): String {
        val isCamera = data == null || data.data == null

        return if (isCamera)
            getCaptureImageOutputUri()!!.getPath()
//            getCaptureImageOutputUri()!!.getAbsolutePath()
        else
            getPathFromURI(data!!.data)

    }

    private fun getCaptureImageOutputUri(): Uri? {
        val getImage = activity!!.getExternalFilesDir("")
        if (getImage != null) {
            outputFileUri = Uri.fromFile(File(getImage.path, "profile.png"))
//            outputFileUri= getImageUriFromBitmap(activity!!,photo!!)
        }
        return outputFileUri
    }

But outputFileUri is always returned null. Can anyone help me with that please?

3

3 Answers

0
votes

while firing intent you can pass extra as MediaStore.EXTRA_OUTPUT as your file location and later on, you will get Uri in on activity result as Uri. Ref:https://stackguides.com/questions/2729267/android-camera-intent

And If you want Uri from the file then you can use FileProvider to get its Uri.

 Uri uri = FileProvider.getUriForFile(context, "com.package.name.fileprovider", file);
0
votes
// Get the image from drawable resource as drawable object
// Get the bitmap from drawable object

// Initializing a new file
// The bellow line return a directory in internal storage
var file: File = Environment.getExternalStoragePublicDirectory("yourFolderName")

if (!file.exists()) {
    // Create a file to save the image
    file.mkdirs()
}

file = File(file, "yourPicName")
try {
    // Get the file output stream
    val stream: OutputStream = FileOutputStream(file)

    stream.use {
        // Compress bitmap
        this.compress(Bitmap.CompressFormat.JPEG, 100, it)

        // Flush the stream
        it.flush()

        // Close stream
        it.close()

        val uri:Uri = file.toUri()

        // OR

        val uri2:Uri = Uri.fromFile(file)
    }
} catch (e: IOException) { // Catch the exception
    e.printStackTrace()
}