[英]Get image from drawable by path
我正在尝试通过路径获取可绘制文件夹内的图像,所以我尝试了这个但不起作用
String path = "android.resource:///" + BuildConfig.APPLICATION_ID + "/drawable/logo_ataturk";
File imgFile = new File(path);
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView imgLogo = findViewById(R.id.imageView_logo);
imgLogo.setImageBitmap(myBitmap);
}
我找到了最简单的解决方案
Uri path = Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/drawable/logo_ataturk");
ImageView imgLogo = findViewById(R.id.imageView_logo);
imgLogo.setImageURI(path);
资源不是设备上的文件。 它们是开发人员计算机上的文件,仅此而已。
您可以将所有这些代码替换为:
ImageView imgLogo = findViewById(R.id.imageView_logo);
imgLogo.setImageResource(R.drawable.logo_ataturk);
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier($name_of_the_image, "drawable", getPackageName()));
要么
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
imgLogo.setImageBitmap(myBitmap);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.