Androidでローカルの画像を読み込む方法は何ですか?
Androidでローカル画像を読み込む方法には、通常2つあります:リソースIDを使用するか、ファイルパスを使用するか。
- リソース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);
}
この方法はファイルシステムから画像を読み込むために適しています。