Java Code Examples for android.content.ContentResolver#getType()

The following examples show how to use android.content.ContentResolver#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SPHelper.java    From timecat with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: MediaStoreRequestHandler.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 3
Source File: StringUtils.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 4
Source File: Submit.java    From Slide with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: MimeType.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
/**
 * 获取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;
}
 
Example 6
Source File: AdapterDownloads.java    From SimplicityBrowser with MIT License 5 votes vote down vote up
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;
}
 
Example 7
Source File: ImportExportActivity.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: Serializer.java    From android with GNU General Public License v3.0 5 votes vote down vote up
/** 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;
}
 
Example 9
Source File: Utils.java    From WhatsApp-Bulk-Sender with MIT License 5 votes vote down vote up
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;
}
 
Example 10
Source File: SPHelper.java    From timecat with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 11
Source File: SPHelper.java    From timecat with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: SPHelper.java    From timecat with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 13
Source File: SPHelper.java    From timecat with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 14
Source File: MediaProcessor.java    From Android-RTEditor with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 15
Source File: SPHelper.java    From timecat with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 16
Source File: UiUtil.java    From android-file-chooser with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 17
Source File: ContentUriUtils.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source File: PBitmapUtils.java    From YImagePicker with Apache License 2.0 4 votes vote down vote up
public static String getMimeTypeFromUri(Activity context, Uri uri) {
    ContentResolver resolver = context.getContentResolver();
    return resolver.getType(uri);
}
 
Example 19
Source File: ContextUtils.java    From memetastic with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 20
Source File: ContextUtils.java    From openlauncher with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}