溫馨提示×

android加載本地圖片的方法是什么

小億
203
2024-03-11 10:04:26
欄目: 編程語言

Android加載本地圖片的方法通常有兩種:使用資源ID或使用文件路徑。

  1. 使用資源ID:
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.image_name);

這種方法適用于將圖片放置在res/drawable目錄下。

  1. 使用文件路徑:
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)中加載圖片。

0