在Assets Android中获取尺寸图像

问题描述 投票:0回答:1

在Asset Android an image.png中,如何获得尺寸?

我试过,但这不起作用。

String image = "my_image.png"

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(context.getAssets() +  "/" + image, options);
 // BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() + "/other_dir/" + image, options);

    int width_t = options.outWidth;
    int height_t = options.outHeight;

如果image在/ other_dir /中,我可以获得维度,但不能在Asset中。

android image assets
1个回答
0
投票

试试这个:

AssetManager asset = context.getAssets();
InputStream is; 
try {
    is = asset.open("my_image.png");
} catch (IOException e) {
return null;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);

int width_t = options.outWidth;
int height_t = options.outHeight;
© www.soinside.com 2019 - 2024. All rights reserved.