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?