从图片的URI获取宽高
Get width and Height from URI of picture
我正在尝试获取图片的宽度和高度,以便能够将其加载到我的 activity 上的 canvas 中。当我尝试打印此高度和宽度时,我总是得到 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);
}
private void getImageWidthAndHeight(Uri uri){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(new File(uri.getPath()).getAbsolutePath(), options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
}
我正在尝试获取图片的宽度和高度,以便能够将其加载到我的 activity 上的 canvas 中。当我尝试打印此高度和宽度时,我总是得到 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);
}
private void getImageWidthAndHeight(Uri uri){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(new File(uri.getPath()).getAbsolutePath(), options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
}