【发布时间】:2017-03-19 07:56:28
【问题描述】:
我正在尝试获取图片的宽度和高度,以便能够将其加载到我的活动的画布中。当我尝试打印此高度和宽度时,我总是得到 0。我不明白为什么?
这是我的代码:
private void openPicture() {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
InputStream stream = context.getContentResolver().openInputStream(uri);
BitmapFactory.decodeStream(stream);
int width = options.outWidth;
int height = options.outHeight;
Log.d("DEBUG", ""+ width);
loadPicture(width, height);
} catch(IOException e) {
Log.e("FingerPainterView", e.toString());
}
}
private void loadPicture(int w, int h) {
try {
// attempt to load the uri provided, scale to fit our canvas
InputStream stream = context.getContentResolver().openInputStream(uri);
Bitmap bm = BitmapFactory.decodeStream(stream);
bitmap = Bitmap.createScaledBitmap(bm, Math.max(w, h), Math.max(w, h), false);
stream.close();
bm.recycle();
} catch(IOException e) {
Log.e("FingerPainterView", e.toString());
}
canvas = new Canvas(bitmap);
}
【问题讨论】: