How to insert an image as a path to a file in an image view

Asked 1 years ago, Updated 1 years ago, 109 views

Put resources in drawable

 ImageView imgView=new ImageView(this);
     imgView.setBackgroundResource(R.drawable.img1);

I set it up in imageView like this. Instead of registering resources in the drawable folder, I want to set the imageView by giving the path of the file. How shall I do it?

android imageview android-imageview android-file

2022-09-22 14:15

1 Answers

If you want to receive the path of the image from SD-Card and register it in ImageView, Using Bitmap

File imgFile = new  File("/sdcard/Images/test_image.jpg");

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

    myImage.setImageBitmap(myBitmap);

}

You can do it like this.

Of course, in the Android manifest file, <uses-permission android:name="android.permission.You must register WRITE_EXTERNAL_STORAGE" />


2022-09-22 14:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.