last day (16 days later) » 

11:59 PM
1
Q: Android Kotlin: Unable to reconstruct File from the URI in onActivityResult

Wai Yan HeinI am building an Android application using the Kotlin programming. In my application, I am building a custom image picker. When the image is picked, the onActivityResult callback function is executed. Then in the function, I am trying to reconstruct File the URI as follow. if (requestCode == PICK...

 
Hi please check my answer here stackoverflow.com/a/66202077/7085389
 
Hi, I tried. But it is still throwing error.
I have updated the question. Please have a look.
 
try with this val bytes = File(getRealPathFromURI(data?.data)).readBytes()
 
Yes, that is exactly what I did. But it is throwing the error I mentioned.
 
please try with this val inputStream: InputStream? = data?.data?.let { contentResolver.openInputStream(it) } val targetArray = inputStream?.let { ByteArray(it.available()) } inputStream?.read(targetArray)
 
11:59 PM
But I need byte array. How can I get it?
 
use the targetArray
 
That one works. Thanks
But it dont think it was converted into the right away. I mean the data does not seem right, when I convert the byte array into base64 string.
 
I converted this Array Byte to Bitmap and I put it on an imageView and its work I did not understand your problem
 
Can you please share your code if possible?
 
yep targetArray?.size?.let { val bmp =BitmapFactory.decodeByteArray(targetArray,0, it) img?.setImageBitmap(bmp) }
 
11:59 PM
I tried. It is not showing anything in the preview
val inputStream: InputStream? = data?.data?.let { contentResolver.openInputStream(it) } val targetArray: ByteArray = inputStream?.let { ByteArray(it.available()) } as ByteArray targetArray?.size?.let { val bmp =BitmapFactory.decodeByteArray(targetArray,0, it) imagePreview?.setImageBitmap(bmp) }
 
you forget this line inputStream.read(targetArray)
  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        // val bytes = File(URI(data?.data?.path)).readBytes()
        // val b = contentResolver
        val inputStream: InputStream? = data?.data?.let { contentResolver.openInputStream(it) }
        val targetArray: ByteArray = inputStream?.let {
            ByteArray(it.available())
        } as ByteArray
        inputStream.read(targetArray)
        targetArray.size.let {
 

  last day (16 days later) »