Home > Software engineering >  Upload image File from file path
Upload image File from file path

Time:07-08

In android studio I have uploaded an image to the drawables folder in resources and not I'm trying to use that image in my project. Im checking to make sure the image file exists, but checking if it exists returns false everytime

        val imgFile = File("/Users/jakesmith/Desktop/Attachments/app/src/main/res/drawable/myimage.png")

        if (imgFile.exists()) {
            val myBitmap = BitmapFactory.decodeFile(imgFile.absolutePath)
            imageView.setImageBitmap(myBitmap)
        }

any suggestions would be greatly appreciated!

CodePudding user response:

This can perform what you want in Java code.

imageView.setImageResource(R.drawable.myimage)

If you want to do it in layout file

<ImageView
    android:id="@ id/sampleimage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/myimage"/>

CodePudding user response:

Use BitmapFactory.decodeResource() as mentioned in comments. And then simply use try catch to avoid ResourceNotFound exception.

val image = BitmapFactory.decodeResource(context.resources, R.drawable.myImage)
imageView.setImageBitmap(image)

  • Related