读取特定密度的mipmap资源

UFC*_*der 2 android

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

在此输入图像描述

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

InputStream ins = getResources().
            openRawResource(getResources().
                    getIdentifier("ic_launcher.png",
            "mipmap-xxhdpi", getPackageName()));
Run Code Online (Sandbox Code Playgroud)

但我得到一个例外,说没有找到资源.做正确的方法是什么?我想特别读取mipmap-xxhdpi中的文件

lel*_*man 6

你可以获得xxhdpi的特定drawable

ResourcesCompat.getDrawableForDensity(getResources(), R.mipmap.ic_launcher, DisplayMetrics.DENSITY_XXHIGH, getTheme());
Run Code Online (Sandbox Code Playgroud)

之后,您可以将位图压缩回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();
Run Code Online (Sandbox Code Playgroud)