jk *_* jk 35 height android uri image width
我可以MediaStore.Images.Media正常获得图像宽度
但我需要从Dropbox中选择的图像中获取图像的宽度和高度
所以目前我有以下方法从Dropbox获取图像大小
private void getDropboxIMGSize(Uri uri){
String size = Long.toString(new File(uri.getPath()).length());
return size;
}
Run Code Online (Sandbox Code Playgroud)
但我真正需要的是获取文件的宽度和高度值
有谁知道如何实现这一目标?请帮忙!
Bla*_*elt 86
private void getDropboxIMGSize(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;
}
Run Code Online (Sandbox Code Playgroud)
没有办法.您必须创建一个Bitmap对象.如果使用该inJustDecodeBounds标志,则不会在momery中加载位图.实际上BitmapFactory.decodeFile将返回null.在我的例子中uri是图像的物理路径
Gaz*_*kus 12
如果您有文件uri,Blackbelt的答案是正确的。但是,如果您像官方相机教程中那样使用新的文件提供程序,则它将不起作用。这适用于这种情况:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(
getContext().getContentResolver().openInputStream(mPhotoUri),
null,
options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
Run Code Online (Sandbox Code Playgroud)
小智 11
Blackbelt的答案大部分时间都可以使用Options,但我想通过使用ExifInterface提出另一种解决方案或后备解决方案.如果您有图像URI,则可以使用完整路径创建ExifInterface,不需要Bitmap对象,也不需要BitmapFactory.Options.
恩.
int width = exif.getAttributeInt( ExifInterface.TAG_IMAGE_WIDTH, defaultValue );
int height = exif.getAttributeInt( ExifInterface.TAG_IMAGE_LENGTH, defaultValue );
Run Code Online (Sandbox Code Playgroud)
这组实用程序与 Android 中的 Size 抽象一起使用。
它包含一个类SizeFromImage.java 你可以这样使用它:
ISize size = new SizeFromImage(imgFilePath);
size.width();
size.hight();
Run Code Online (Sandbox Code Playgroud)