What is the method for loading local images on Android?

There are typically two methods for loading local images on Android: using a resource ID or using a file path.

  1. Resource ID:
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.image_name);

This method is suitable for placing images in the res/drawable directory.

  1. Use the file path:
ImageView imageView = findViewById(R.id.imageView);
String imagePath = "/path/to/image.jpg";
File imgFile = new  File(imagePath);
if(imgFile.exists()){
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    imageView.setImageBitmap(myBitmap);
}

This method is suitable for loading images from the file system.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds