android.content.ContentResolver#getType ( )源码实例Demo

下面列出了android.content.ContentResolver#getType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: timecat   文件: SPHelper.java
@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;
}
 
源代码2 项目: 365browser   文件: ContentUriUtils.java
/**
 * 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);
}
 
源代码3 项目: DoraemonKit   文件: MediaStoreRequestHandler.java
@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);
}
 
源代码4 项目: android-file-chooser   文件: UiUtil.java
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;
}
 
源代码5 项目: timecat   文件: SPHelper.java
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;
}
 
源代码6 项目: Android-RTEditor   文件: MediaProcessor.java
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);
}
 
源代码7 项目: timecat   文件: SPHelper.java
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);
}
 
源代码8 项目: timecat   文件: SPHelper.java
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);
}
 
源代码9 项目: timecat   文件: SPHelper.java
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);
}
 
源代码10 项目: timecat   文件: SPHelper.java
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);
    }
}
 
源代码11 项目: WhatsApp-Bulk-Sender   文件: Utils.java
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;
}
 
源代码12 项目: android   文件: Serializer.java
/** 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;
}
 
源代码13 项目: budget-watch   文件: ImportExportActivity.java
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;
}
 
源代码14 项目: SimplicityBrowser   文件: AdapterDownloads.java
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;
}
 
源代码15 项目: PictureSelector   文件: MimeType.java
/**
 * 获取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;
}
 
源代码16 项目: Slide   文件: Submit.java
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;
}
 
源代码17 项目: q-municate-android   文件: StringUtils.java
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;
}
 
源代码18 项目: YImagePicker   文件: PBitmapUtils.java
public static String getMimeTypeFromUri(Activity context, Uri uri) {
    ContentResolver resolver = context.getContentResolver();
    return resolver.getType(uri);
}
 
源代码19 项目: memetastic   文件: ContextUtils.java
/**
 * 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;
}
 
源代码20 项目: openlauncher   文件: ContextUtils.java
/**
 * 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;
}