Example usage for android.content ContentResolver SCHEME_CONTENT

List of usage examples for android.content ContentResolver SCHEME_CONTENT

Introduction

In this page you can find the example usage for android.content ContentResolver SCHEME_CONTENT.

Prototype

String SCHEME_CONTENT

To view the source code for android.content ContentResolver SCHEME_CONTENT.

Click Source Link

Usage

From source file:dev.nick.app.screencast.camera.MediaScratchFileProvider.java

public static boolean isMediaScratchSpaceUri(final Uri uri) {
    if (uri == null) {
        return false;
    }/*from   www.  j  av  a 2s.com*/

    final List<String> segments = uri.getPathSegments();
    return (TextUtils.equals(uri.getScheme(), ContentResolver.SCHEME_CONTENT)
            && TextUtils.equals(uri.getAuthority(), AUTHORITY) && segments.size() == 1
            && isValidFileId(segments.get(0)));
}

From source file:com.android.messaging.datamodel.MediaScratchFileProvider.java

public static boolean isMediaScratchSpaceUri(final Uri uri) {
    if (uri == null) {
        return false;
    }// w w w  .j  av  a  2  s . c  o m

    final List<String> segments = uri.getPathSegments();
    return (TextUtils.equals(uri.getScheme(), ContentResolver.SCHEME_CONTENT)
            && TextUtils.equals(uri.getAuthority(), AUTHORITY) && segments.size() == 1
            && FileProvider.isValidFileId(segments.get(0)));
}

From source file:Main.java

/**
 * Try to get the exif orientation of the passed image uri
 * //www.ja v  a 2 s .c o  m
 * @param context
 * @param uri
 * @return
 */
public static int getExifOrientation(Context context, Uri uri) {

    final String scheme = uri.getScheme();

    ContentProviderClient provider = null;
    if (scheme == null || ContentResolver.SCHEME_FILE.equals(scheme)) {
        return getExifOrientation(uri.getPath());
    } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
        try {
            provider = context.getContentResolver().acquireContentProviderClient(uri);
        } catch (SecurityException e) {
            return 0;
        }

        if (provider != null) {
            Cursor result;
            try {
                result = provider.query(uri,
                        new String[] { Images.ImageColumns.ORIENTATION, Images.ImageColumns.DATA }, null, null,
                        null);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }

            if (result == null) {
                return 0;
            }

            int orientationColumnIndex = result.getColumnIndex(Images.ImageColumns.ORIENTATION);
            int dataColumnIndex = result.getColumnIndex(Images.ImageColumns.DATA);

            try {
                if (result.getCount() > 0) {
                    result.moveToFirst();

                    int rotation = 0;

                    if (orientationColumnIndex > -1) {
                        rotation = result.getInt(orientationColumnIndex);
                    }

                    if (dataColumnIndex > -1) {
                        String path = result.getString(dataColumnIndex);
                        rotation |= getExifOrientation(path);
                    }
                    return rotation;
                }
            } finally {
                result.close();
            }
        }
    }
    return 0;
}

From source file:Main.java

/**
 * Try to get the exif orientation of the passed image uri
 *
 * @param context/* ww  w.  j a v a 2s. c o  m*/
 * @param uri
 * @return
 */
public static int getExifOrientation(Context context, Uri uri) {

    final String scheme = uri.getScheme();

    ContentProviderClient provider = null;
    if (scheme == null || ContentResolver.SCHEME_FILE.equals(scheme)) {
        return getExifOrientation(uri.getPath());
    } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
        try {
            provider = context.getContentResolver().acquireContentProviderClient(uri);
        } catch (SecurityException e) {
            return 0;
        }

        if (provider != null) {
            Cursor result;
            try {
                result = provider.query(uri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION,
                        MediaStore.Images.ImageColumns.DATA }, null, null, null);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }

            if (result == null) {
                return 0;
            }

            int orientationColumnIndex = result.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION);
            int dataColumnIndex = result.getColumnIndex(MediaStore.Images.ImageColumns.DATA);

            try {
                if (result.getCount() > 0) {
                    result.moveToFirst();

                    int rotation = 0;

                    if (orientationColumnIndex > -1) {
                        rotation = result.getInt(orientationColumnIndex);
                    }

                    if (dataColumnIndex > -1) {
                        String path = result.getString(dataColumnIndex);
                        rotation |= getExifOrientation(path);
                    }
                    return rotation;
                }
            } finally {
                result.close();
            }
        }
    }
    return 0;
}

From source file:com.android.messaging.datamodel.MediaScratchFileProvider.java

public static Uri.Builder getUriBuilder() {
    return (new Uri.Builder()).authority(AUTHORITY).scheme(ContentResolver.SCHEME_CONTENT);
}

From source file:Main.java

static String convertUriToPath(Context context, Uri uri) {
    Log.v(TAG, "convertUriToPath : " + uri + " @" + context);

    String path = null;/*from   ww w. jav a 2s . co  m*/
    if (null != uri) {
        String scheme = uri.getScheme();
        if (null == scheme || scheme.equals("") || scheme.equals(ContentResolver.SCHEME_FILE)) {
            path = uri.getPath();
        } else if (scheme.equals("http")) {
            path = uri.toString();
        } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
            String[] projection = new String[] { MediaStore.MediaColumns.DATA };
            Cursor cursor = null;
            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                if (null == cursor || 0 == cursor.getCount() || !cursor.moveToFirst()) {
                    throw new IllegalArgumentException("Given Uri could not be found in media store");
                }
                int pathIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                path = cursor.getString(pathIndex);
            } catch (SQLiteException e) {
                throw new IllegalArgumentException(
                        "Given Uri is not formatted in a way so that it can be found in media store.");
            } finally {
                if (null != cursor) {
                    cursor.close();
                }
            }
        } else {
            throw new IllegalArgumentException("Given Uri scheme is not supported");
        }
    }

    Log.v(TAG, "convertUriToPath : >" + path);
    return path;
}

From source file:com.danjarvis.documentcontract.DocumentContract.java

/**
 * Creates a new file from the data resolved through the provided content URI.
 *
 * @return name of created file (residing at cordova.file.dataDirectory).
 *///from w  ww  .  ja  v  a  2 s .  com
private void createFile(JSONObject args, CallbackContext callback) {
    try {
        Uri uri;
        String fileName;
        ContentResolver contentResolver;
        InputStream is;
        FileOutputStream fs;
        byte[] buffer;
        int read = 0;

        uri = getUri(args);
        if (null == uri || !(uri.getScheme().equals(ContentResolver.SCHEME_CONTENT))) {
            callback.error(INVALID_URI_ERROR);
            return;
        }

        fileName = getFileName(args);
        if (null == fileName) {
            callback.error(INVALID_PARAMS_ERROR);
            return;
        }

        contentResolver = cordova.getActivity().getContentResolver();
        if (null == contentResolver) {
            callback.error("Failed to get ContentResolver object.");
            return;
        }

        is = contentResolver.openInputStream(uri);
        fs = cordova.getActivity().openFileOutput(fileName, Context.MODE_PRIVATE);

        buffer = new byte[32768];
        while ((read = is.read(buffer, 0, buffer.length)) != -1) {
            fs.write(buffer, 0, read);
        }

        fs.close();
        fs.flush();
        is.close();

        callback.success(fileName);
    } catch (FileNotFoundException fe) {
        callback.error(fe.getMessage());
    } catch (IOException ie) {
        callback.error(ie.getMessage());
    }
}

From source file:com.tojc.ormlite.android.annotation.info.ContentUriInfo.java

public Uri getContentUri() {
    return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(this.authority)
            .appendPath(this.path).build();
}

From source file:Main.java

private static boolean isMediaUri(Uri uri) {
    return uri != null && ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())
            && MediaStore.AUTHORITY.equals(uri.getAuthority());
}

From source file:com.commonsware.android.tte.DocumentStorageService.java

private void load(Uri document) {
    try {/*www.  j av a  2s  .c o  m*/
        boolean weHavePermission = false;
        boolean isContent = ContentResolver.SCHEME_CONTENT.equals(document.getScheme());

        if (isContent) {
            int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;

            getContentResolver().takePersistableUriPermission(document, perms);

            for (UriPermission perm : getContentResolver().getPersistedUriPermissions()) {
                if (perm.getUri().equals(document)) {
                    weHavePermission = true;
                }
            }
        } else {
            weHavePermission = true;
        }

        if (weHavePermission) {
            try {
                InputStream is = getContentResolver().openInputStream(document);

                try {
                    String text = slurp(is);
                    DocumentFile docFile;

                    if (isContent) {
                        docFile = DocumentFile.fromSingleUri(this, document);
                    } else {
                        docFile = DocumentFile.fromFile(new File(document.getPath()));
                    }

                    EventBus.getDefault().post(
                            new DocumentLoadedEvent(document, text, docFile.getName(), docFile.canWrite()));
                } finally {
                    is.close();
                }
            } catch (Exception e) {
                Log.e(getClass().getSimpleName(), "Exception loading " + document.toString(), e);
                EventBus.getDefault().post(new DocumentLoadErrorEvent(document, e));
            }
        } else {
            Log.e(getClass().getSimpleName(), "We failed to get permissions for " + document.toString());
            EventBus.getDefault().post(new DocumentPermissionFailureEvent(document));
        }
    } catch (SecurityException e) {
        Log.e(getClass().getSimpleName(), "Exception getting permissions for " + document.toString(), e);
        EventBus.getDefault().post(new DocumentPermissionFailureEvent(document));
    }
}