读取特定密度的mipmap资源

时间:2017-01-28 12:40:38

标签: 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 中的文件,特别是

1 个答案:

答案 0 :(得分:6)

您可以使用

获取xxhdpi的特定drawable
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();
相关问题