Example usage for android.net Uri getAuthority

List of usage examples for android.net Uri getAuthority

Introduction

In this page you can find the example usage for android.net Uri getAuthority.

Prototype

@Nullable
public abstract String getAuthority();

Source Link

Document

Gets the decoded authority part of this URI.

Usage

From source file:com.pavlospt.rxfile.RxFile.java

private static boolean isGoogleDriveDocument(Uri uri) {
    return Constants.GOOGLE_DRIVE_DOCUMENT_AUTHORITY.equals(uri.getAuthority());
}

From source file:com.iceteck.silicompressorr.FileUtils.java

/**
 * Get a file path from a Uri. This will get the the path for Storage Access
 * Framework Documents, as well as the _data field for the MediaStore and
 * other file-based ContentProviders.<br>
 * <br>//from w  w w . j  ava2  s .c om
 * Callers should check whether the path is local before assuming it
 * represents a local file.
 *
 * @param context The context.
 * @param uri The Uri to query.
 * @see #isLocal(String)
 * @see #getFile(Context, Uri)
 * @author paulburke
 */
public static String getPath(final Context context, final Uri uri) {

    if (DEBUG)
        Log.d(TAG + " File -",
                "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: "
                        + uri.getPort() + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme()
                        + ", Host: " + uri.getHost() + ", Segments: " + uri.getPathSegments().toString());

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, uri)) {

        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }

            // TODO handle non-primary volumes
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:org.odk.collect.android.utilities.MediaUtils.java

/**
 * @param uri The Uri to check/*from w  w  w  .  j a v a 2s .  c o m*/
 * @return Whether the Uri authority is Google Drive.
 */
public static boolean isGoogleDriveDocument(Uri uri) {
    return "com.google.android.apps.docs.storage".equals(uri.getAuthority())
            || uri.getAuthority().startsWith("com.google.android.apps.photos.content");
}

From source file:org.totschnig.myexpenses.util.FileUtils.java

/**
 * Get a file path from a Uri. This will get the the path for Storage Access
 * Framework Documents, as well as the _data field for the MediaStore and
 * other file-based ContentProviders.<br>
 * <br>//from   w  w  w.j a  v a 2 s  .co m
 * Callers should only use this for display purposes and not for accessing the file directly via the
 * file system
 *
 * @param context The context.
 * @param uri The Uri to query.
 * @see #isLocal(String)
 * @see #getFile(Context, Uri)
 * @author paulburke
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {

    if (DEBUG)
        Log.d(TAG + " File -",
                "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: "
                        + uri.getPort() + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme()
                        + ", Host: " + uri.getHost() + ", Segments: " + uri.getPathSegments().toString());

    // DocumentProvider
    if (isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                String path = Environment.getExternalStorageDirectory().getPath();
                if (split.length > 1) {
                    path += "/" + split[1];
                }
                return path;
            }
            //there is no documented way of returning a path to a file on non primary storage.
            //so what we do is displaying the documentId to the user which is better than just null
            return docId;
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:com.activiti.android.platform.provider.transfer.ContentTransferManager.java

public static final AddContentRelatedRepresentation prepareTransfer(Intent resultData, AlfrescoFragment fr,
        String id, int type) {
    Uri uri = null;
    if (resultData != null) {
        uri = resultData.getData();/* ww  w .ja v  a 2  s.  c  o  m*/
        Log.i("TAG", "uri: " + uri);

        // Detect if it comes from Alfresco ?
        if (AlfrescoIntegrator.STORAGE_ACCESS_PROVIDER.equals(uri.getAuthority())) {
            // It's alfresco !
            // Let's see if it's possible to link...
            AddContentRelatedRepresentation content = prepareLink(fr, uri);
            if (content == null) {
                // Link is impossible we upload the document
                requestUpload(fr, uri, id, type, resultData.getType());
            } else {
                // Link is possible.
                // Let's request the user on how to handle this
                uploadAs(fr, uri, content, id, type, resultData.getType());
            }
        } else {
            // It's something else
            requestUpload(fr, uri, id, type, resultData.getType());
        }
    }
    return null;
}

From source file:com.portol.common.utils.Util.java

/**
 * Merges a uri and a string to produce a new uri.
 * <p>//  w  w  w  .  j  a  v a 2  s  .c o  m
 * The uri is built according to the following rules:
 * <ul>
 * <li>If {@code baseUri} is null or if {@code stringUri} is absolute, then {@code baseUri} is
 * ignored and the uri consists solely of {@code stringUri}.
 * <li>If {@code stringUri} is null, then the uri consists solely of {@code baseUrl}.
 * <li>Otherwise, the uri consists of the concatenation of {@code baseUri} and {@code stringUri}.
 * </ul>
 *
 * @param baseUri A uri that can form the base of the merged uri.
 * @param stringUri A relative or absolute uri in string form.
 * @return The merged uri.
 */
public static Uri getMergedUri(Uri baseUri, String stringUri) {
    if (stringUri == null) {
        return baseUri;
    }
    if (baseUri == null) {
        return Uri.parse(stringUri);
    }
    if (stringUri.startsWith("/")) {
        stringUri = stringUri.substring(1);
        return new Uri.Builder().scheme(baseUri.getScheme()).authority(baseUri.getAuthority())
                .appendEncodedPath(stringUri).build();
    }
    Uri uri = Uri.parse(stringUri);
    if (uri.isAbsolute()) {
        return uri;
    }
    return Uri.withAppendedPath(baseUri, stringUri);
}

From source file:com.github.chenxiaolong.dualbootpatcher.FileUtils.java

@NonNull
public static DocumentFile getDocumentFile(@NonNull Context context, @NonNull Uri uri) {
    DocumentFile df = null;/*  w  ww  .  jav a  2 s .c  om*/

    if (FILE_SCHEME.equals(uri.getScheme())) {
        df = DocumentFile.fromFile(new File(uri.getPath()));
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && SAF_SCHEME.equals(uri.getScheme())
            && SAF_AUTHORITY.equals(uri.getAuthority())) {
        if (DocumentsContract.isDocumentUri(context, uri)) {
            df = DocumentFile.fromSingleUri(context, uri);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && DocumentsContract.isTreeUri(uri)) {
            df = DocumentFile.fromTreeUri(context, uri);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // Best guess is that it's a tree...
            df = DocumentFile.fromTreeUri(context, uri);
        }
    }

    if (df == null) {
        throw new IllegalArgumentException("Invalid URI: " + uri);
    }

    return df;
}

From source file:fpt.isc.nshreport.utilities.FileUtils.java

/**
 * Get a file path from a Uri. This will get the the path for Storage Access
 * Framework Documents, as well as the _data field for the MediaStore and
 * other file-based ContentProviders.<br>
 * <br>//from  w w  w  .j  a v  a2  s  .  com
 * Callers should check whether the path is local before assuming it
 * represents a local file.
 *
 * @param context The context.
 * @param uri     The Uri to query.
 * @author paulburke
 * @see #isLocal(String)
 * @see #getFile(Context, Uri)
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {

    if (DEBUG)
        Log.d(TAG + " File -",
                "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: "
                        + uri.getPort() + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme()
                        + ", Host: " + uri.getHost() + ", Segments: " + uri.getPathSegments().toString());

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // LocalStorageProvider
        //if (isLocalStorageDocument(uri)) {
        if (false) {
            // The path is the id
            return DocumentsContract.getDocumentId(uri);
        }
        // ExternalStorageProvider
        else if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }

            // TODO handle non-primary volumes
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:Main.java

public static String encodeUrl(String url) {
    Uri uri = Uri.parse(url);

    try {/*from  w  w w  .j a va2  s  .  c o m*/
        Map<String, List<String>> splitQuery = splitQuery(uri);
        StringBuilder encodedQuery = new StringBuilder();
        for (String key : splitQuery.keySet()) {
            for (String value : splitQuery.get(key)) {
                if (encodedQuery.length() > 0) {
                    encodedQuery.append("&");
                }
                encodedQuery.append(key + "=" + URLEncoder.encode(value, "UTF-8"));
            }
        }
        String queryString = encodedQuery != null && encodedQuery.length() > 0 ? "?" + encodedQuery : "";

        URI baseUri = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), null, uri.getFragment());
        return baseUri + queryString;
    } catch (UnsupportedEncodingException ignore) {
    } catch (URISyntaxException ignore) {
    }

    return uri.toString();
}

From source file:com.futurologeek.smartcrossing.crop.CropImageActivity.java

public static boolean isGooglePhotosUri(Uri uri) {
    return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}