Android加载本地图片的方法通常有两种:使用资源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); }
这种方法适用于从文件系统中加载图片。