Android加載本地圖片的方法通常有兩種:使用資源ID或使用文件路徑。
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.image_name);
這種方法適用于將圖片放置在res/drawable目錄下。
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);
}
這種方法適用于從文件系統(tǒng)中加載圖片。