如何从特定的 mipmap 图像中读取字节?例如,我正在尝试从位于 mipmap-xxhdpi
的 ic_launcher.png 读取字节这是我正在尝试做的事情:
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/