Convert Image from RecyclerView to Bitmap - android

I need to send a specific image from my recyclerview that has been clicked to the new activity that will display the image. The question is how do I convert my image to bitmap or something like that so I can send them using intent?
Here's my code:
RecyclerViewAdapter.kt
class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
fun bindItem(items: Item) {
itemView.name.text = items.name
itemView.desc.text = items.desc
Glide.with(itemView.context).load(items.image).into(itemView.image)
itemView.setOnClickListener {
itemView.context.startActivity(itemView.context.intentFor<DetailsActivity>("image" to bitmap, "name" to items.name, "desc" to items.desc))
}
}
}
SecondActivity
val intent = intent
name = intent.getStringExtra("name")
image = intent.getStringExtra("image")
desc = intent.getStringExtra("desc")
nameTextView.text = name
descTextView.text = desc
Glide.with(this)
.load(image)
.into(imgPhotos)
I have already tried all the code that are provided on this site, but they all didn't work. Thanks!

I am not using Glide but it should be like this.
lateinit imageBitmap : Bitmap
Glide.with(this).asBitmap().load("YOUR_URL").into(object: Target<Bitmap>{
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
imageBitmap = resource
}
})
You can send Bitmap directly because it is Parcelable
intent.putExtra("image",imageBitmap)
To getBitmap
val bitmap = intent.extras.getParcelable<Bitmap>("image")

Convert the image to byteArray and pass it through intent. Extract the data through intent on the other side and convert byteArray into bitmap.

Related

Passing Bitmap through activities

I have two activities and I am trying to pass image by using Serializable way. How to do this? Is it possible to pass image by using Serializable way? Any ideas please.
val resultImage = findViewById<ImageView>(R.id.resultImage)
val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
resultImage.setImageURI(uri)
}
val galleryBtn = findViewById<Button>(R.id.galleryBtn)
val nextBtn = findViewById<Button>(R.id.nextBtn)
galleryBtn.setOnClickListener {
getContent.launch("image/*")
}
nextBtn.setOnClickListener {
val takeImage = resultImage.setImageURI(Uri)
val person = Person ()
Intent(this,SecoendActivity::class.java).also {
it.putExtra("EXTRA_PERSON",person)
startActivity(it)
}
}
In kotlin class file:
data class Person(
val imageUrl: Bitmap
): Serializable
My second activity:
val imageView = findViewById<ImageView>(R.id.imageView)
val person = intent.getSerializableExtra("EXTRA_PERSON")as Person
It is not recommended to use Serializable to pass images between activities, as it can cause memory and performance issues. Instead, you can use the Intent.putExtra method to pass the image's URI to the other activity, and then use the ContentResolver to retrieve the image in the second activity.
// In the first activity
val imageUri = Uri.parse("content://...")
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("image_uri", imageUri)
startActivity(intent)
// In the second activity
val imageUri = intent.getParcelableExtra<Uri>("image_uri")
val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri)

How to parse an URL String to URI or Bitmap and use it for CropImageView in Kotlin

I'm trying to create an image crop functionality using CropImageView from ArthurHub's Library and I have a string URL like https://mywebsite.com/library/folder/KeYO8Ff31l178kO.png. Is possible to parse this link to URI or Bitmap so I can use setImageUriAsync or setImageBitmap to set the image? I'm using Kotlin. Thanks
LATER EDIT:
I found the solution, I'm not sure if it's the best but maybe will be useful:
I created this inner class and inside it I parsed the url to bitmap and in OnPostExecute method I set the resource for cropImageView.
Code:
internal class GetBitmapFromUrlAsync(private val cropImageView: CropImageView) : AsyncTask<String, Void?, Bitmap>() {
override fun doInBackground(vararg src: String): Bitmap? {
val url = URL(src[0])
val bitmap: Bitmap
try {
val connection = url.openConnection() as HttpsURLConnection
connection.doInput = true
connection.connect()
val input: InputStream = connection.inputStream
bitmap = BitmapFactory.decodeStream(input)
} catch (e: Exception) {
e.printStackTrace()
return null
}
return bitmap
}
override fun onPostExecute(bitmap: Bitmap?) {
cropImageView.setImageBitmap(bitmap)
}
}

Cannot load image from URI using picasso

I a having a problem in load picture in an imageview through uri. I am using picasso and my code is as below:
I have an object CreateImage with a function displayImageFromURI
fun displayImageFromURI(imageView: ImageView,uri: Uri?,context: Context){
Log.e("url", uri.toString())
Picasso.get()
.load(uri)
.fit().centerCrop()
.into(imageView, object: com.squareup.picasso.Callback {
override fun onSuccess() {
val imageBitmap = (imageView.drawable as BitmapDrawable).bitmap
val imageDrawable =
RoundedBitmapDrawableFactory.create(context.resources, imageBitmap)
imageView.setImageDrawable(imageDrawable)
}
override fun onError(e: java.lang.Exception?) {
Log.e("error",e.toString())
}
})
}
I am calling this function from a fragment as below:
if (!photoUrl.equals("")) {
Log.e("in photo url", photoUrl)
val image = Uri.parse(photoUrl)
CreateImage.displayImageFromURI(ivUploadQuestionImage, image, this)
}
but my image is not being loaded in the imageview. The string photoUrl contains the link in string format as well. Can anybody please help me?

Pass Bitmap image to another activity in Android App

Hi I want to pass bitmap from activity 1 to activity 2. It is a large bitmap and I can not use a bundle. Now I save bitmap and pass path and read a path and load bitmap I do this :
val path = saveImage(bitmap)
intent.putExtra("bitmap_path", path)
startActivity(intent)
and in activity2
val f = File(intent.getStringExtra("bitmap_path"))
val bitmap= BitmapFactory.decodeFile(f.absolutePath)
How I can do this better I thing about static object bitmap but I know it is a good practice
edit I do this:
class Helper {
var bitmap: Bitmap? = null
companion object {
val instance = Helper()
}
}

Image path failed to store into Object class

In fragment A, I trying to get image object from server and store them into totalListImage. This steps works fine.
var totalListImage: MutableList<Image> = mutableListOf()
fun getWorkRequestImage() {
GlobalScope.launch(Dispatchers.Main) {
val request = WebApi.getImages(activity, obj?.id!!)
request?.images?.let {
for (i in it!!.iterator()) {
totalListImage?.add(i)
}
}
mImageListAdapter.notifyDataSetChanged()
}
}
In same fragment, I allow user to take image and store the captured image into totalListImage.
Here onActivityResult
var path = data?.getExtras()?.getString("bitmap")
if (path != null) {
Log.d(TAG,path)
val images = Image()
images.image?.url = path
totalListImage.add(images)
display()
}
Finally in display method, I want to display all the url. But I get null pointer on the last captured image.
fun display() {
for (i in totalListImage) {
Log.d(TAG, i.image?.url)
}
}
If I remove the captured image, I get this
D/AFragment: https://xxx/image/633/1562424667277.png
D/AFragment: https://xxx/image/637/1562426838223.png
The path of the captured image confirm not null, as I display it I saw this
D/AFragment: /data/user/0/xxxk/cache/images/1562437144046.png
Why I can't save the last image's path into images.image?.url ?
Images
class Images : Serializable {
#PrimaryKey
var id = ""
#Ignore
var image : Image?=null
}
Image
#Parcelize
class Image(
#ColumnInfo(name = "image")
var url: String ? =null
) : Serializable,Parcelable
Referring to this code:
val images = Image()
images.image?.url = path
totalListImage.add(images)
and assuming that you meant val images = Images() (otherwise this snippet won't even compile),
then the issue is that images.image is null.
That's because of the definition of the Images class, where you have var image : Image?=null.
To solve you either:
change the class definition to have a not-null default value, like:
var image : Image? = Image()
or manually initialize it in the snippet above, like:
val images = Images()
images.image = Image()
images.image?.url = path
totalListImage.add(images)

Categories

Resources