检查使用edmodo裁剪器库的选定文件是否是图像



使用内置的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;
    }

但是,对于一加 X,它再次返回 false。是否有任何其他方法(不是资源密集型(来检查获取的文件是否是图像?

因此,UI中没有显示任何内容,我也无法知道它是否是图像。

ContentResolver上呼叫getType(),传入Uri。如果 MIME 类型以 image/ 开头,则它是一个图像。

最新更新