使用内置的CropImageActivity选择图像时,选择器会显示从某个外部应用程序(我的案例为WPS Office)中选择文件(任何类型)的选项。因此,当我选择一个pdf文件(例如)时,onSetImageUriComplete()不会导致任何错误。
@Override
public void onSetImageUriComplete(CropImageView cropImageView, Uri uri, Exception error) {
progressView.setVisibility(View.INVISIBLE);
if (error != null) {
Log.e(LOG_TAG, "Failed to load image for cropping", error);
AndroidUtils.showToast("Unable to upload", this);
finish();
}
}
对于此类情况,错误不为空。因此,UI中没有显示任何内容,我也无法知道它是否是图像。
我尝试从URI检查扩展名,但在某些模型中(根据我的情况,为One Plus X),uri不一定包含扩展名。
我也尝试将uri加载到文件中,然后使用以下代码进行检查:
public static boolean isImage(File file) {
if (file == null || !file.exists()) {
return false;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getPath(), options);
return options.outWidth != -1 && options.outHeight != -1;
}
但是,对于One Plus X,它再次返回false。有没有其他方法(这不是资源密集型)来检查所提取的文件是否是图像?
答案 0 :(得分:1)
因此,UI中没有显示任何内容,我也无法知道它是否是图像。
致电getType()
on a ContentResolver
,传递Uri
。如果MIME类型以image/
开头,则为图像。