0
我想要獲取widht和圖片的高度,然後能夠將其加載到我的活動畫布上。當我嘗試打印這個高度和寬度時,我總是得到0.我不明白爲什麼?從圖片的URI獲取寬度和高度
這裏是我的代碼:
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);
}