android - 读取特定密度的 mipmap 资源

标签 android

如何从特定的 mipmap 图像中读取字节?例如,我正在尝试从位于 mipmap-xxhdpi

ic_launcher.png 读取字节

enter image description here

这是我正在尝试做的事情:

InputStream ins = getResources().
            openRawResource(getResources().
                    getIdentifier("ic_launcher.png",
            "mipmap-xxhdpi", getPackageName()));

但是我得到一个异常,说找不到资源。正确的做法是什么?我特别想阅读 mipmap-xxhdpi 中的文件

最佳答案

您可以通过以下方式获取 xxhdpi 的特定绘图

ResourcesCompat.getDrawableForDensity(getResources(), R.mipmap.ic_launcher, DisplayMetrics.DENSITY_XXHIGH, getTheme());

之后你可以制作一个位图并将其压缩回png

Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
drawable.draw(new Canvas(bitmap));

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] pngBytes = stream.toByteArray();

关于android - 读取特定密度的 mipmap 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41909901/

相关文章:

java - 重新编码图像时出现内存不足异常

android - 如何在AlertDialog android中获取RadioGroup的选定索引

Android setHorizo​​ntalFadingEdgeEnabled 不适用于三星

java - 如何使用 Onclick 更改按钮背景

java - 无法使用共享首选项保存游戏高分?

java - 如何获取java文件中的XML字符串?

android - OutlineTextField 、TextField 在 Jetpack Compose 1.0.0-alpha02 中不起作用

android - 检查应用程序是否实际使用 list 文件中提到的权限

android - 如何在android中设置alljoyn sdk?

java.lang.NullPointerException : Attempt to invoke virtual method on a null object reference Retrofit