組み込みのCropImageActivityを使用して画像を選択している間、ピッカーは外部アプリケーション(私の場合はWPS Office)からファイルを選択するオプションを表示します。したがって、私がpdfファイルを選択すると(例えば)、onSetImageUriComplete()はエラーを起こさない。edmodo cropperライブラリを使用して選択したファイルが画像であるかどうかをチェック
@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();
}
}
このような場合、エラーはnullではありません。ですから、UIには何も表示されず、画像であるかどうかを知る方法がありません。
私はURIからの拡張子をチェックしようとしましたが、いくつかのモデル(私の場合はOne Plus X)では、uriには必ずしも拡張子が含まれていません。
私はまた、ファイルへのURIをロードしようとしたし、このコードを使用して確認してください:falseを返す
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。取得したファイルがイメージであるかどうかをチェックする他の方法(リソースを大量に消費しない方法)はありますか?
あなたはこれを意味します: 'String mimeType = context.getContentResolver()。getType(imageUri);'。これは私のためにnullを返します –
@AvinashGupta:本当ですか? 「ウリ」の価値は何ですか? – CommonsWare
file:///storage/emulated/0/DCIM/Camera/IMG_20170122_162307.jpg –