これを試しましたか?それは私のために働く(画像ファイルのためにのみ)。 MIMEタイプを決定するために失敗した場合にはnullを返します
public static String getMimeTypeOfFile(String pathName) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, opt);
return opt.outMimeType;
}
:
public static String getMimeTypeOfUri(Context context, Uri uri) {
BitmapFactory.Options opt = new BitmapFactory.Options();
/* The doc says that if inJustDecodeBounds set to true, the decoder
* will return null (no bitmap), but the out... fields will still be
* set, allowing the caller to query the bitmap without having to
* allocate the memory for its pixels. */
opt.inJustDecodeBounds = true;
InputStream istream = context.getContentResolver().openInputStream(uri);
BitmapFactory.decodeStream(istream, null, opt);
istream.close();
return opt.outMimeType;
}
もちろん、あなたはまた、他の方法で、このようなようなBitmapFactory.decodeFile
やBitmapFactory.decodeResource
を使用することができます。
http://stackoverflow.com/a/30765320/2765497 – Flinbor