【Android】使用Assets目录中的图片资源
ImageView 中有个setImageBitmap的方法,可以将Bitmap类直接设置为使用的图片资源。
// 设置图片// 获取DOM
ImageView img = findViewById(R.id.image);// 这里的两个参数,改成适合自己使用场景的
// 参数1, 当前的上下文
// 参数2,filePath, 要读取的文件名
img.setImageBitmap(getAssetsBitmap(MainActivity.fileName));public static Bitmap getAssetsBitmap(Context context, String path) {AssetManager am = context.getAssets();InputStream inputStream = null;try {inputStream = am.open(path);} catch (IOException e) {e.printStackTrace();}Bitmap bitmap = BitmapFactory.decodeStream(inputStream);return bitmap;
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
