Linux 拨号vps windows公众号手机端

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

lewis 9年前 (2016-12-11) 阅读数 10 #程序编程
文章标签 android

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

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

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门