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.
- 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.
- 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.