下面列出了android.content.ContentResolver#getType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static Set<String> getStringSet(String name, Set<String> defaultValue) {
checkContext();
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.parse(CONTENT_URI + SEPARATOR + TYPE_STRING_SET + SEPARATOR + name);
String rtn = cr.getType(uri);
if (rtn == null || rtn.equals(NULL_STRING)) {
return defaultValue;
}
if (!rtn.matches("\\[.*\\]")){
return defaultValue;
}
String sub=rtn.substring(1,rtn.length()-1);
String[] spl=sub.split(", ");
Set<String> returns=new HashSet<>();
for (String t:spl){
returns.add(t.replace(COMMA_REPLACEMENT,", "));
}
return returns;
}
/**
* Retrieve the MIME type for the content URI.
*
* @param uriString the content URI to look up.
* @return MIME type or null if the input params are empty or invalid.
*/
@CalledByNative
public static String getMimeType(String uriString) {
ContentResolver resolver = ContextUtils.getApplicationContext().getContentResolver();
Uri uri = Uri.parse(uriString);
if (isVirtualDocument(uri)) {
String[] streamTypes = resolver.getStreamTypes(uri, "*/*");
return (streamTypes != null && streamTypes.length > 0) ? streamTypes[0] : null;
}
return resolver.getType(uri);
}
@Override public Result load(Request request, int networkPolicy) throws IOException {
ContentResolver contentResolver = context.getContentResolver();
int exifOrientation = getExifOrientation(contentResolver, request.uri);
String mimeType = contentResolver.getType(request.uri);
boolean isVideo = mimeType != null && mimeType.startsWith("video/");
if (request.hasSize()) {
PicassoKind picassoKind = getPicassoKind(request.targetWidth, request.targetHeight);
if (!isVideo && picassoKind == FULL) {
return new Result(null, getInputStream(request), DISK, exifOrientation);
}
long id = parseId(request.uri);
BitmapFactory.Options options = createBitmapOptions(request);
options.inJustDecodeBounds = true;
calculateInSampleSize(request.targetWidth, request.targetHeight, picassoKind.width,
picassoKind.height, options, request);
Bitmap bitmap;
if (isVideo) {
// Since MediaStore doesn't provide the full screen kind thumbnail, we use the mini kind
// instead which is the largest thumbnail size can be fetched from MediaStore.
int kind = (picassoKind == FULL) ? Video.Thumbnails.MINI_KIND : picassoKind.androidKind;
bitmap = Video.Thumbnails.getThumbnail(contentResolver, id, kind, options);
} else {
bitmap =
Images.Thumbnails.getThumbnail(contentResolver, id, picassoKind.androidKind, options);
}
if (bitmap != null) {
return new Result(bitmap, null, DISK, exifOrientation);
}
}
return new Result(null, getInputStream(request), DISK, exifOrientation);
}
public static String getMimeType(@NonNull Context ctx, Uri uri) {
String mimeType;
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
ContentResolver cr = ctx.getApplicationContext().getContentResolver();
mimeType = cr.getType(uri);
} else {
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
fileExtension.toLowerCase());
}
return mimeType;
}
public static String getString(String name, String defaultValue) {
checkContext();
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.parse(CONTENT_URI + SEPARATOR + TYPE_STRING + SEPARATOR + name);
String rtn = cr.getType(uri);
if (rtn == null || rtn.equals(NULL_STRING)) {
return defaultValue;
}
return rtn;
}
protected String getMimeType() throws IOException, Exception {
if (mOriginalFile.startsWith("content://")) {
// ContentProvider file
ContentResolver resolver = RTApi.getApplicationContext().getContentResolver();
Uri uri = Uri.parse(mOriginalFile);
return resolver.getType(uri);
}
String extension = FilenameUtils.getExtension(mOriginalFile);
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
public static float getFloat(String name, float defaultValue) {
checkContext();
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.parse(CONTENT_URI + SEPARATOR + TYPE_FLOAT + SEPARATOR + name);
String rtn = cr.getType(uri);
if (rtn == null || rtn.equals(NULL_STRING)) {
return defaultValue;
}
return Float.parseFloat(rtn);
}
public static boolean getBoolean(String name, boolean defaultValue) {
checkContext();
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.parse(CONTENT_URI + SEPARATOR + TYPE_BOOLEAN + SEPARATOR + name);
String rtn = cr.getType(uri);
if (rtn == null || rtn.equals(NULL_STRING)) {
return defaultValue;
}
return Boolean.parseBoolean(rtn);
}
public static long getLong(String name, long defaultValue) {
checkContext();
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.parse(CONTENT_URI + SEPARATOR + TYPE_LONG + SEPARATOR + name);
String rtn = cr.getType(uri);
if (rtn == null || rtn.equals(NULL_STRING)) {
return defaultValue;
}
return Long.parseLong(rtn);
}
public static boolean contains(String name) {
checkContext();
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.parse(CONTENT_URI + SEPARATOR + TYPE_CONTAIN + SEPARATOR + name);
String rtn = cr.getType(uri);
if (rtn == null || rtn.equals(NULL_STRING)) {
return false;
} else {
return Boolean.parseBoolean(rtn);
}
}
public static String getMimeType(Context context, File file) {
String mimeType = null;
Uri uri = Uri.fromFile(file);
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
ContentResolver cr = context.getContentResolver();
mimeType = cr.getType(uri);
} else {
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
.toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
fileExtension.toLowerCase());
}
return mimeType;
}
/** Convert an Uri to JSON object.
*
* Object will include:
* "type" of data;
* "uri" itself;
* "path" to the file, if applicable.
* "data" for the file.
*/
public static JSONObject toJSONObject(
final ContentResolver contentResolver,
final Uri uri)
throws JSONException {
if (uri == null) {
return null;
}
final JSONObject json = new JSONObject();
final String type = contentResolver.getType(uri);
json.put("type", type);
json.put("uri", uri);
json.put("path", getRealPathFromURI(contentResolver, uri));
return json;
}
public String getMimeType(Uri uri)
{
String mimeType = null;
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
if(fileExtension != null)
{
fileExtension = fileExtension.toLowerCase();
for(DataFormat format : DataFormat.values())
{
if(fileExtension.equals(format.name().toLowerCase()))
{
mimeType = format.mimetype();
break;
}
}
}
if(mimeType == null && uri.getScheme() != null && uri.getScheme().equals(ContentResolver.SCHEME_CONTENT))
{
ContentResolver cr = getContentResolver();
mimeType = cr.getType(uri);
if(mimeType != null)
{
if(mimeType.equals("text/comma-separated-values"))
{
mimeType = "text/csv";
}
}
}
return mimeType;
}
private String getMimeType(Uri uri) {
String mimeType;
if (Objects.equals(uri.getScheme(), ContentResolver.SCHEME_CONTENT)) {
ContentResolver cr = context.getContentResolver();
mimeType = cr.getType(uri);
} else {
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
.toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
fileExtension.toLowerCase());
}
return mimeType;
}
/**
* 获取mimeType
*
* @param context
* @param uri
* @return
*/
public static String getMimeTypeFromMediaContentUri(Context context, Uri uri) {
String mimeType;
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
ContentResolver cr = context.getContentResolver();
mimeType = cr.getType(uri);
} else {
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
.toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
fileExtension.toLowerCase());
}
return TextUtils.isEmpty(mimeType) ? "image/jpeg" : mimeType;
}
public File createFile(Uri uri, @NonNull Context context) {
InputStream in;
ContentResolver resolver = context.getContentResolver();
String type = resolver.getType(uri);
String extension;
if ("image/png".equals(type)) {
extension = ".gif";
} else if ("image/png".equals(type)) {
extension = ".png";
} else {
extension = ".jpg";
}
try {
in = resolver.openInputStream(uri);
} catch (FileNotFoundException e) {
return null;
}
// Create files from a uri in our cache directory so they eventually get deleted
String timeStamp = String.valueOf(System.currentTimeMillis());
File cacheDir = ((Reddit) context.getApplicationContext()).getImageLoader()
.getDiskCache()
.getDirectory();
File tempFile = new File(cacheDir, timeStamp + extension);
if (writeInputStreamToFile(in, tempFile)) {
return tempFile;
} else {
// If writeInputStreamToFile fails, delete the excess file
tempFile.delete();
}
return null;
}
public static String getMimeType(Uri uri) {
String mimeType;
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
ContentResolver cr = App.getInstance().getContentResolver();
mimeType = cr.getType(uri);
} else {
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
.toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
fileExtension.toLowerCase());
}
return mimeType;
}
public static String getMimeTypeFromUri(Activity context, Uri uri) {
ContentResolver resolver = context.getContentResolver();
return resolver.getType(uri);
}
/**
* Detect MimeType of given file
* Android/Java's own MimeType map is very very small and detection barely works at all
* Hence use custom map for some file extensions
*/
public String getMimeType(final Uri uri) {
String mimeType = null;
if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
ContentResolver cr = _context.getContentResolver();
mimeType = cr.getType(uri);
} else {
String ext = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext.toLowerCase());
// Try to guess if the recommended methods fail
if (TextUtils.isEmpty(mimeType)) {
switch (ext) {
case "md":
case "markdown":
case "mkd":
case "mdown":
case "mkdn":
case "mdwn":
case "rmd":
mimeType = "text/markdown";
break;
case "yaml":
case "yml":
mimeType = "text/yaml";
break;
case "json":
mimeType = "text/json";
break;
case "txt":
mimeType = "text/plain";
break;
}
}
}
if (TextUtils.isEmpty(mimeType)) {
mimeType = "*/*";
}
return mimeType;
}
/**
* Detect MimeType of given file
* Android/Java's own MimeType map is very very small and detection barely works at all
* Hence use custom map for some file extensions
*/
public String getMimeType(final Uri uri) {
String mimeType = null;
if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
ContentResolver cr = _context.getContentResolver();
mimeType = cr.getType(uri);
} else {
String ext = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext.toLowerCase());
// Try to guess if the recommended methods fail
if (TextUtils.isEmpty(mimeType)) {
switch (ext) {
case "md":
case "markdown":
case "mkd":
case "mdown":
case "mkdn":
case "mdwn":
case "rmd":
mimeType = "text/markdown";
break;
case "yaml":
case "yml":
mimeType = "text/yaml";
break;
case "json":
mimeType = "text/json";
break;
case "txt":
mimeType = "text/plain";
break;
}
}
}
if (TextUtils.isEmpty(mimeType)) {
mimeType = "*/*";
}
return mimeType;
}