0

通常、次のコードを使用して画像の幅を取得できますが、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));
4

1 に答える 1

7

境界をデコードするだけのオプションをファクトリに渡します。

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;

また

ImageViewとを使用してgetWidth()高さと幅を取得できますがgetHeight()、これでは画像の正確な幅と高さは得られません。画像の幅の高さを取得するには、まず背景としてドローアブルを取得する必要があり、次にドローアブルをBitmapDrawable` に変換して取得しますここのように幅と高さを取得できるビットマップとしての画像

Bitmap b = ((BitmapDrawble)imageView.getBackground()).getBitmap();
int w = b.getWidth();
int h = b.getHeight();

またはこのようにする

imageView.setDrawingCacheEnabled(true);
Bitmap b = imageView.getDrawingCache();
int w = b.getWidth();
int h = b.getHeight();

上記のコードはImageview、デバイスのスクリーンショットのような現在のサイズのビットマップを提供します

ImageViewサイズのみ

imageView.getWidth();
imageView.getHeight();

描画可能な画像があり、そのサイズが必要な場合は、このように取得できます

Drawable d = getResources().getDrawable(R.drawable.yourimage);
int h = d.getIntrinsicHeight();
int w = d.getIntrinsicWidth();
于 2013-07-25T14:46:26.277 に答える