Androidでローカルの画像を読み込む方法は何ですか?

Androidでローカル画像を読み込む方法には、通常2つあります:リソース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);
}

この方法はファイルシステムから画像を読み込むために適しています。

コメントを残す 0

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


广告
広告は10秒後に閉じます。
bannerAds