android:minSdkVersion ="8"时如何获取图像的高度和宽度

Hel*_*oCW 0 android

通常,我可以使用以下代码来获取图像的宽度,但它需要API级别16.如何在android:minSdkVersion ="8"时获取图像的高度和宽度

Cursor cur = mycontext.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,
                MediaStore.Images.Media._ID + "=?", new String[] { id }, "");
string width=cur.getString(cur.getColumnIndex(MediaStore.Images.Media.HEIGHT));
Run Code Online (Sandbox Code Playgroud)

Ank*_*wal 6

传递选项只是解码工厂的边界:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

//Returns null, sizes are in the options variable
BitmapFactory.decodeFile("/sdcard/image.png", options);
int width = options.outWidth;
int height = options.outHeight;
//If you want, the MIME type will also be decoded (if possible)
String type = options.outMimeType;
Run Code Online (Sandbox Code Playgroud)

要么

你可以ImageView通过使用getWidth()getHeight()通过获得高度和宽度虽然这不会给你图像的确切宽度和高度,为了获得图像宽度高度首先你需要将drawable作为背景然后将drawable转换为BitmapDrawable` to get the image as Bitmap from that you can get the width and height like here

Bitmap b = ((BitmapDrawble)imageView.getBackground()).getBitmap();
int w = b.getWidth();
int h = b.getHeight();
Run Code Online (Sandbox Code Playgroud)

or do like this way

imageView.setDrawingCacheEnabled(true);
Bitmap b = imageView.getDrawingCache();
int w = b.getWidth();
int h = b.getHeight();
Run Code Online (Sandbox Code Playgroud)

the above code will give you current Imageview大小的位图,如设备的屏幕截图

仅限ImageView尺寸

imageView.getWidth();
imageView.getHeight();
Run Code Online (Sandbox Code Playgroud)

如果你有可绘制的图像,并且你想要这个尺寸,你就可以这样做

Drawable d = getResources().getDrawable(R.drawable.yourimage);
int h = d.getIntrinsicHeight();
int w = d.getIntrinsicWidth();
Run Code Online (Sandbox Code Playgroud)