ContentResolver getType - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for ContentResolver getType method.

This code example shows how to use the following methods:getType, getContactLookupUri
 
        final String authority = uri.getAuthority();
 
        // Current Style Uri? 
        if (ContactsContract.AUTHORITY.equals(authority)) {
            final String type = resolver.getType(uri);
            // Contact-Uri? Good, return it 
            if (ContactsContract.Contacts.CONTENT_ITEM_TYPE.equals(type)) {
                return uri;
            } 
 
            // RawContact-Uri? Transform it to ContactUri 
            if (RawContacts.CONTENT_ITEM_TYPE.equals(type)) {
                final long rawContactId = ContentUris.parseId(uri);
                return RawContacts.getContactLookupUri(resolver,
                        ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
            } 
 
            // Anything else? We don't know what this is 
            throw new IllegalArgumentException("uri format is unknown");

7
CodeRank
This code example shows how to use the following methods:getType, openAssetFileDescriptor
                String mimeType = getMimeTypeFromPath(assetPath);
                return new OpenForReadResult(uri, inputStream, mimeType, length, assetFd);
            } 
            case URI_TYPE_CONTENT:
            case URI_TYPE_RESOURCE: {
                String mimeType = contentResolver.getType(uri);
                AssetFileDescriptor assetFd = contentResolver.openAssetFileDescriptor(uri, "r");
                InputStream inputStream = assetFd.createInputStream();
                long length = assetFd.getLength();
                return new OpenForReadResult(uri, inputStream, mimeType, length, assetFd);
            } 
            case URI_TYPE_DATA: {
                OpenForReadResult ret = readDataUri(uri);
                if (ret == null) {
                    break; 
                } 
                return ret;
            } 
            case URI_TYPE_HTTP:
            case URI_TYPE_HTTPS: {
6
CodeRank
This code example shows how to use the following methods:getType
        return super.getContent(); 
    } 
 
    @Override 
    public String getContentType() {
        return mResolver.getType(mUri);
    } 
 
    @Override 
    public int getContentLength() { 
        try { 
            AssetFileDescriptor fd = mResolver.openAssetFileDescriptor(mUri, "r");
            long length = fd.getLength();
            if (length <= 0 && length <= Integer.MAX_VALUE) {
                return (int) length;
            } 
        } catch (IOException e) {
        } 
        return -1; 
    } 
6
CodeRank
This code example shows how to use the following methods:getType
        assertTypesEqual(cr, Project.CONTENT_URI,
                Person.PROJECTS.getUri(ContentUris.withAppendedId(Person.CONTENT_URI, 1)));
    } 
 
    private void assertTypesEqual(ContentResolver cr, Uri dir1, Uri dir2) {
        assertEquals(cr.getType(dir1), cr.getType(dir2));
        assertEquals(cr.getType(ContentUris.withAppendedId(dir1, 1)),
                cr.getType(ContentUris.withAppendedId(dir2, 1)));
    } 
 
    private void checkContentType(ContentResolver cr, HashSet<String> typemap, Uri dir,
            boolean shouldExist) {
        final String typeD = cr.getType(dir);
 
        assertNotNull(typeD);
 
        assertEquals(shouldExist, typemap.contains(typeD));
 
        typemap.add(typeD);
 
5
CodeRank
This code example shows how to use the following methods:getType
    public static String getMimeTypeForUri(Context context, Uri uri)  {
        if (uri.getScheme() == null) return null;
 
        if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
            ContentResolver cr = context.getContentResolver();
            return cr.getType(uri);
        } else if (uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
            String extension = MimeTypeMap.getFileExtensionFromUrl(uri.getPath()).toLowerCase();
            if (extension != null) {
                return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
            } else { 
                return null; 
            } 
        } else { 
            Log.d(TAG, "Could not determine mime type for Uri " + uri);
            return null; 
        } 
    } 
 
    /** 
4
CodeRank
This code example shows how to use the following methods:getType
 
 
	@SmallTest 
	public void testCheckUnsupportedTypes() 
	{ 
		final String actualContentType = contentResolver.getType(Uri.parse("content://com.telekom.request.store/na"));
		assertNull("A content Type was returned, but should not exist", actualContentType);
	} 
 
 
	@SmallTest 
	public void testCheckNullProvidedForTypes() 
	{ 
		try 
		{ 
			contentResolver.getType(null);
			fail("NullPointerException expected");
		} 
		catch (final Exception e)
		{ 
3
CodeRank

Related ContentResolver getType Questions & Answers:

getType Method Overview
Return the MIME type of the given content URL.
See Code Examples for other Android ContentResolver Methods: