Android Image setImageBitmap not working when using glide load image - android

I have a problem using Glide load image android.I will present it simply as follows:
First I load image using glide
I want to choose image using Intent.ACTION_PICK but when I using image.setImageBitmap on onActivityResult but it not working
Glide.with(context!!).load(url).centerCrop().error(R.drawable.avata_boy).into(imgAvata)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK
&& data != null && data.data != null
) {
filePath = data.data
try {
val bitmap = BitmapFactory.decodeStream(activity!!.contentResolver.openInputStream(filePath!!))
imgAvata.setImageBitmap(bitmap)
Glide.with(context!!)
.load(bitmap)
.placeholder(R.drawable.avata_boy)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(imgAvata)
} catch (e: IOException) {
}

Try this!!
val myBitmap = BitmapFactory.decodeFile(filePath!!.getAbsolutePath())
imgview.setImageBitmap(myBitmap)

Related

onActivityResults reseting values

I'm testing one functionality of an app in different devices and it happens in one of them:
When I calls onActivityResult, it works normally but in the second time it's called, it reset the previous data registered in the last call. It works normally in most of devices but this one exceptionally cleans the view when comes from activity result.
So basically when I selected the second image either from gallery or for camera intent, it resets the previous selected file.
This is how the data it's being retrieved:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(resultCode == RESULT_OK) {
val selectedImage: Uri?
if(requestCode == GALLERY_1 || requestCode == GALLERY_2 || requestCode == GALLERY_3) {
selectedImage = data!!.data
configImagePersistentPermission(selectedImage)
} else {
val bitmap = data!!.extras!!.get("data") as Bitmap
selectedImage = configImagePathUri(requireContext(), bitmap)
}
try {
when (requestCode) {
GALLERY_1 -> {
binding.imageDoc1.setImageURI(selectedImage)
configResetImageView(0)
}
CAMERA_1 -> {
binding.imageDoc1.setImageURI(selectedImage)
configResetImageView(0)
}
GALLERY_2 -> {
binding.imageDoc2.setImageURI(selectedImage)
configResetImageView(1)
}
CAMERA_2 -> {
binding.imageDoc2.setImageURI(selectedImage)
configResetImageView(1)
}
GALLERY_3 -> {
binding.imageDoc3.setImageURI(selectedImage)
configResetImageView(2)
}
CAMERA_3 -> {
binding.imageDoc3.setImageURI(selectedImage)
configResetImageView(2)
}
}
imageList.add(selectedImage.toString())
} catch (e: Exception){
e.printStackTrace()
}
}
}
How to prevent losing the previous recorded data in this scenario?
Thanks in advance

onActivityResult not getting called after changing the intent for (Save the full-size photo)

Im following the official documentation to capture the image and gets its path.
https://developer.android.com/training/camera/photobasics
I have write 100% same code as the documentation say, but two weird things are happing when I write the simple intent
private fun dispatchTakePictureIntent() {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}
}
The OnAcivityResult is getting called
But when I change the intent into
private fun dispatchTakePictureIntent() {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
// Ensure that there's a camera activity to handle the intent
takePictureIntent.resolveActivity(packageManager)?.also {
// Create the File where the photo should go
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
// Error occurred while creating the File
...
null
}
// Continue only if the File was successfully created
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(
this,
"com.example.android.fileprovider",
it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}
}
}
}
It does get called anymore.
Here is my OnAcivityResult code
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == OPEN_CAMERA)
if(resultCode == RESULT_OK && data != null) {
// not getting called
}
if(requestCode == OPEN_GALLERY)
if(resultCode == RESULT_OK && data != null) {
val image: Uri = data.data!!
openCropImageView(image)
}
if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
val uri = UCrop.getOutput(data!!)
val imgFile = File(uri?.path)
if (imgFile.exists()) {
val myBitmap = BitmapFactory.decodeFile(imgFile.absolutePath)
binding.previewImage.setImageBitmap(myBitmap)
}
}
}
any help, please.
Edit: Apparently its working fine on a pixel 5 running on android 11
emulator and its not working on Redmi note 5 running on android 8

How can I pass a variable control name to findViewById in Kotlin?

I am trying to learn Kotlin and I'm building a simple example as I go. I have 3 image buttons that open the camera and take a photo. The thumbnail is then set into an ImageView. I've used the examples from https://developer.android.com/training/camera/photobasics?hl=en to get the basics working (figuring if I can make it work for one, it'll work for all. It does indeed work for one, but I can't figure out how to make it one function that drops the thumbnail into the correct ImageView.
Inside my onCreate I have the listener for each of the buttons that will invoke the camera:
camRead1.setOnClickListener {dispatchTakePictureIntent() }
camRead2.setOnClickListener {dispatchTakePictureIntent() }
camRead3.setOnClickListener {dispatchTakePictureIntent() }
And I took the sample from the url above:
val REQUEST_IMAGE_CAPTURE = 1
private fun dispatchTakePictureIntent() {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val thumb: ImageView = findViewById(R.id.thumbRead1)
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
val imageBitmap = data.extras.get("data") as Bitmap
thumb.setImageBitmap(imageBitmap)
}
}
and pasted it into my class MainActivity, and after I replaced imageView in the override function with a variable (thumb) and added the super, it worked perfectly for the first one.
However, I am trying to get 3 photos, read1, read2, and read3 which each need to display the thumb in thumbRead1, thumbRead2 and thumbRead3. I can't figure out how the onActivityResult is executed since the call inside dispatchTakePictureIntent is calling startActivityForResult (especially as Android Studio says that startActivityForResult is deprecated).
Obviously, once onActivityResult executes, I can see that thumb defines R.id.thumbRead1 and receives imageBitmap but I don't understand how I can make it aware of the button that was clicked.
Without understanding how onActivityResult is called, I'm thinking that if I can do something like:
findViewById(R.id("thumbRead" + imgID))
to define the specific ImageView that I want the photo pasted into. Am I on the right track here? If not, what is the recommended way of doing this?
Note they've recently added what's supposed to be a cleaner way of starting other activities for results and getting the results, explained here. But since you're already doing it the traditional way, I'll explain how to get that working.
I think the easiest thing to do in this situation is just make more request codes, so you can check which request it was.
val REQUEST_IMAGE_CAPTURE_SOURCE_1 = 1
val REQUEST_IMAGE_CAPTURE_SOURCE_2 = 2
val REQUEST_IMAGE_CAPTURE_SOURCE_3 = 3
//...
camRead1.setOnClickListener { dispatchTakePictureIntent(REQUEST_IMAGE_CAPTURE_SOURCE_1) }
camRead2.setOnClickListener { dispatchTakePictureIntent(REQUEST_IMAGE_CAPTURE_SOURCE_2) }
camRead3.setOnClickListener { dispatchTakePictureIntent(REQUEST_IMAGE_CAPTURE_SOURCE_3) }
//...
private fun dispatchTakePictureIntent(requestCode: Int) {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, requestCode)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode != RESULT_OK) {
// possibly show message to user
return
}
val imageViewId = when (requestCode) {
REQUEST_IMAGE_CAPTURE_SOURCE_1 -> R.id.thumbRead1
REQUEST_IMAGE_CAPTURE_SOURCE_2 -> R.id.thumbRead2
REQUEST_IMAGE_CAPTURE_SOURCE_3 -> R.id.thumbRead3
}
val imageView = findViewById<ImageView>(imageViewId)
imageView.imageBitmap = data.extras.get("data") as Bitmap
}
By the way, if you want to get an ID for a view using the String like you were showing you were trying, you would do it like this:
val viewId = resources.getIdentifier("thumbRead$imgId", "id", packageName)
val imageView = findViewById<ImageView>(viewId)
You need to pass different request code for each call and pass it to the dispatchTakePictureIntent function. You do not need to get id by findviewbyid. You simply can add the image on the basis of the request code.
val REQUEST_IMAGE_CAPTURE_ONE = 1
val REQUEST_IMAGE_CAPTURE_TWO = 2
val REQUEST_IMAGE_CAPTURE_THREE = 3
private fun dispatchTakePictureIntent(requestCode: Int) {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, requestCode)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
val imageBitmap = data.extras.get("data") as Bitmap
if (requestCode == REQUEST_IMAGE_CAPTURE_ONE ) {
thumbRead1.setImageBitmap(imageBitmap)
}else if (requestCode == REQUEST_IMAGE_CAPTURE_TWO ) {
thumbRead2.setImageBitmap(imageBitmap)
}else if (requestCode == REQUEST_IMAGE_CAPTURE_THREE ) {
thumbRead3.setImageBitmap(imageBitmap)
}
}
}

uploading image from gallery and camera to server

I need to save the image that is taken from camera or image taken from gallery in kotlin. for now I've done this part and i'm finding other examples in kotlin but i'm unable to find. In other examples, images were send in multipart which was coded in java.
These are listeners
private val cameraRequest = 1888
private val pickImage = 100
private var imageUri: Uri? = null
takePhoto.setOnClickListener {
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, cameraRequest)
}
choosePicture.setOnClickListener {
val gallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
startActivityForResult(gallery, pickImage)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == cameraRequest) {
val photo: Bitmap = data?.extras?.get("data") as Bitmap
takePhoto.setImageBitmap(photo)
}
if (resultCode == RESULT_OK && requestCode == pickImage) {
imageUri = data?.data
if (imageUri != null) {
choosePicture.setImageURI(imageUri)
}
}
}
You can use this library for making a multipart request: https://github.com/gotev/android-upload-service
Also, it will handle app behaviour for you which includes upload-retry on slow internet connection and many more.

getData() is not working since migration to kotlin

I am new to kotlin. I wrote this code that will get image from image picker.
But I am getting error on line:val filePath: Uri = attr.data.getData()
error: Unresolved reference. Is there any change in kotlin because this code was working properly in java (Means I migrated to kotlin)
And another error on imageStore(bitmap) error: smart cast to bitmap is impossible.
I've searched for documentation but couldn't solve this 2 problems .
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == 1) {
val filePath: Uri = attr.data.getData()
try {
val inputStream: InputStream? = contentResolver.openInputStream(filePath)
bitmap = BitmapFactory.decodeStream(inputStream)
imageStore(bitmap)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
}
}
But I am getting error on line:val filePath: Uri = attr.data.getData() error: Unresolved reference
Well, this means the compiler can't understand what you're referring to. Is it correct to say this ?
Well, considering you don't have any object named attr it would seem that the compiler is correct.
this means you should remove the attr part, something like:
val filePath: Uri = data?.getData() ?: return
The return here will stop executing the rest of the method if the data from the intent is null
To resolve your second problem, you'll need something like this :
bitmap?.let { bitmapInstance ->
imageStore(bitmapInstance)
}
What does this do ?
Well, it gives you thread safe access to your bitmap object and also ensures that the instance isn't null.
As #a_local_nobody said "the migration tool isn't perfect", I learned the basics of kotlin and found that the following code works fine for me.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
val filePath = data.data
try {
val inputStream = contentResolver.openInputStream(filePath!!)
bitmap = BitmapFactory.decodeStream(inputStream)
imageView!!.setImageBitmap(bitmap)
imageStore(bitmap)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
}
super.onActivityResult(requestCode, resultCode, data)
}
Replace this line val filePath: Uri = attr.data.getData() with
val filePath: Uri = data!!.getData()!!
Updated code
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == 1) {
val filePath: Uri = data!!.getData()!!
try {
val inputStream: InputStream? = contentResolver.openInputStream(filePath)
var bitmap = BitmapFactory.decodeStream(inputStream)
imageStore(bitmap)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
}
}

Categories

Resources