android加载本地图片的方法是什么

1042
2024/2/25 10:04:26
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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);
}

这种方法适用于从文件系统中加载图片。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: android防止反编译的方法是什么