2017-02-13 89 views
0

使用内置的CropImageActivity选择图像时,选择器显示从某些外部应用程序(WPS Office)中选择文件(任何类型)的选项。所以当我选择一个PDF文件(比如说)时,onSetImageUriComplete()不会导致任何错误。检查使用edmodo裁剪库的选定文件是否为图像

@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(); 
     } 
    } 

对于这种情况,错误不为空。所以,在用户界面中什么也没有显示,我也没有办法知道它是否是一个图像。

我试着从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。是否有任何其他方式(这不是资源密集型)来检查获取的文件是否是图像?

回答

1

所以,在UI中什么也没有显示出来,我也没有办法知道它是否是图像。

拨打getType() on a ContentResolver,传递Uri。如果MIME类型以image/开头,则它是一个图像。

+0

您的意思是:'String mimeType = context.getContentResolver()。getType(imageUri);'。这对我返回null –

+0

@AvinashGupta:真的吗? 'Uri'的价值是什么? – CommonsWare

+0

file:///storage/emulated/0/DCIM/Camera/IMG_20170122_162307.jpg –