Search code examples
androidimageassets

Get dimensions image in Assets Android


In Asset Android an image.png, how can get dimensions?

I've try, this but doesn't work.

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;

If image is in /other_dir/, I can get dimensions, but not in Asset.


Solution

  • Try this:

    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;