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:Main.java

public static void shiftTableUriIds(HashMap<String, ArrayList<ContentValues>> operationMap, String tableName,
        String idColumnName, String authority, String path, long topTableId) {
    ArrayList<ContentValues> restoreOperations = operationMap.get(tableName);
    if (null == restoreOperations) {
        return;//from  ww  w .  ja v  a  2s  . c  om
    }
    for (ContentValues restoreCv : restoreOperations) {
        if (restoreCv.containsKey(idColumnName) && null != restoreCv.getAsString(idColumnName)) {

            Uri uri = Uri.parse(restoreCv.getAsString(idColumnName));
            if ("content".equalsIgnoreCase(uri.getScheme()) && authority.equals(uri.getAuthority())
                    && uri.getPath().substring(0, uri.getPath().lastIndexOf('/')).equals(path)) {
                Uri.Builder newUri = uri.buildUpon()
                        .path(uri.getPath().substring(0, uri.getPath().lastIndexOf('/')))
                        .appendPath(String.valueOf(
                                Long.parseLong(uri.getPath().substring(uri.getPath().lastIndexOf('/') + 1))
                                        + topTableId));

                //               Log.v("URI", uri.getPath().substring(uri.getPath().lastIndexOf('/')+1));
                //               Log.v("URI", String.valueOf(Long.parseLong(uri.getPath().substring(uri.getPath().lastIndexOf('/')+1)) + topTableId));

                restoreCv.put(idColumnName, newUri.build().toString());
            }
        }
    }
}

From source file:Main.java

/**
 * @param uri The Uri to check.//  w  ww . j a v  a  2 s.  c  o  m
 * @return Whether the Uri authority is MediaProvider.
 */
private static boolean isMediaDocument(Uri uri) {
    return "com.android.providers.media.documents".equals(uri.getAuthority());
}

From source file:Main.java

/**
 * @param uri The Uri to check./* ww w.j  a  va 2s  .com*/
 * @return Whether the Uri authority is Google Photos.
 */
private static boolean isGooglePhotosUri(Uri uri) {
    return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}

From source file:Main.java

/**
 * @param uri The Uri to check./*w  w w  .j  a  v  a 2  s. c  o m*/
 * @return Whether the Uri authority is DownloadsProvider.
 */
private static boolean isDownloadsDocument(Uri uri) {
    return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}

From source file:Main.java

public static Uri getImageUrlWithAuthority(Context context, Uri uri) {
    InputStream is = null;/*from  w w  w  . j  ava  2  s  .  co  m*/

    try {

        if (uri.getAuthority() == null) {
            return null;
        } else if (uri.getAuthority() != null) {
            try {
                is = context.getContentResolver().openInputStream(uri);
                Bitmap bmp = BitmapFactory.decodeStream(is);

                bitmapResult = bmp;
                return writeToTempImageAndGetPathUri(context, bmp);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/**
 * Get a file path from a Uri./*w ww.  j  ava2 s.  com*/
 * 
 * @param context
 * @param uri
 * @return
 * @throws URISyntaxException
 * 
 * @author paulburke
 */
public static String getPath(Context context, Uri uri) throws URISyntaxException {

    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());

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    }

    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:id.nci.stm_9.FileHelper.java

/**
 * Get a file path from a Uri./*from  ww  w .j  a v a 2  s  . c  o m*/
 * 
 * from https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/
 * afilechooser/utils/FileUtils.java
 * 
 * @param context
 * @param uri
 * @return
 * 
 * @author paulburke
 */
public static String getPath(Context context, Uri uri) {
    Log.d("Stm-9" + " File -",
            "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: " + uri.getPort()
                    + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme() + ", Host: " + uri.getHost()
                    + ", Segments: " + uri.getPathSegments().toString());

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    }

    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:org.sufficientlysecure.keychain.helper.FileHelper.java

/**
 * Get a file path from a Uri.//from  w w w  .  j a v  a  2s. co  m
 * <p/>
 * from https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/
 * afilechooser/utils/FileUtils.java
 *
 * @param context
 * @param uri
 * @return
 * @author paulburke
 */
public static String getPath(Context context, Uri uri) {
    Log.d(Constants.TAG + " File -",
            "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: " + uri.getPort()
                    + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme() + ", Host: " + uri.getHost()
                    + ", Segments: " + uri.getPathSegments().toString());

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                int columnIndex = cursor.getColumnIndexOrThrow("_data");
                return cursor.getString(columnIndex);
            }
        } catch (Exception e) {
            // Eat it
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:org.thialfihar.android.apg.helper.FileHelper.java

/**
 * Get a file path from a Uri.//ww  w.ja  v  a  2  s .c  o  m
 *
 * from https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/
 * afilechooser/utils/FileUtils.java
 *
 * @param context
 * @param uri
 * @return
 *
 * @author paulburke
 */
public static String getPath(Context context, Uri uri) {
    Log.d(Constants.TAG + " File -",
            "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: " + uri.getPort()
                    + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme() + ", Host: " + uri.getHost()
                    + ", Segments: " + uri.getPathSegments().toString());

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int columnIndex = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(columnIndex);
            }
        } catch (Exception e) {
            // Eat it
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:Main.java

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

    try {//ww  w  .  j  a  v  a2  s .c  o m
        String query = uri.getQuery();
        String encodedQuery = query != null ? URLEncoder.encode(query, "UTF-8") : null;
        URI tmp = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), null, uri.getFragment());
        return tmp + (encodedQuery != null && encodedQuery.length() > 0 ? "?" + encodedQuery : "");
    } catch (UnsupportedEncodingException ignore) {
    } catch (URISyntaxException ignore) {
    }

    return uri.toString();
}