Example usage for android.net Uri getPath

List of usage examples for android.net Uri getPath

Introduction

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

Prototype

@Nullable
public abstract String getPath();

Source Link

Document

Gets the decoded path.

Usage

From source file:Main.java

protected static String getImagePath(Uri imageUri, Context context) {
    String scheme = imageUri.getScheme();
    if (scheme.equals("file"))
        return imageUri.getPath();
    Cursor cursor = null;// w w  w .j  a v  a  2s.co  m
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(imageUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null)
            cursor.close();
    }

}

From source file:Main.java

public static String getPathFromUri(Uri uri) {
    if (uri == null) {
        return null;
    }/*from   w w w. j  a va2  s . c  om*/
    String scheme = uri.getScheme();
    if (scheme != null && scheme.equals("file")) {
        String filePath = uri.getPath();
        return filePath;
    }
    return null;

}

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

public static File getFileFromUri(final Uri uri) {
    Assert.equals(AUTHORITY, uri.getAuthority());
    return getFileWithExtension(uri.getPath(), getExtensionFromUri(uri));
}

From source file:Main.java

public static File getFromMediaUri(ContentResolver resolver, Uri uri) {
    if (uri == null)
        return null;

    if (SCHEME_FILE.equals(uri.getScheme())) {
        return new File(uri.getPath());
    } else if (SCHEME_CONTENT.equals(uri.getScheme())) {
        final String[] filePathColumn = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME };
        Cursor cursor = null;//  w  w  w .jav a  2  s.  c  o m
        try {
            cursor = resolver.query(uri, filePathColumn, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int columnIndex = (uri.toString().startsWith("content://com.google.android.gallery3d"))
                        ? cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)
                        : cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
                // Picasa image on newer devices with Honeycomb and up
                if (columnIndex != -1) {
                    String filePath = cursor.getString(columnIndex);
                    if (!TextUtils.isEmpty(filePath)) {
                        return new File(filePath);
                    }
                }
            }
        } catch (SecurityException ignored) {
            // Nothing we can do
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
    return null;
}

From source file:Main.java

public static String getRealFilePath(Context context, Uri uri) {
    if (null == uri)
        return null;
    String scheme = uri.getScheme();
    String data = null;/*from   www  . jav a  2s  .  c  o  m*/
    if (scheme == null)
        data = uri.getPath();
    else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        data = uri.getPath();
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        Cursor cursor = context.getContentResolver().query(uri, new String[] { ImageColumns.DATA }, null, null,
                null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}

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

/**
 * Returns a uri that can be used to access a raw mms file.
 *
 * @return the URI for an raw mms file/*w  ww.  j  a v a2s  .  c  o m*/
 */
public static Uri buildMediaScratchSpaceUri(final String extension) {
    final Uri uri = FileProvider.buildFileUri(AUTHORITY, extension);
    final File file = getFileWithExtension(uri.getPath(), extension);
    if (!ensureFileExists(file)) {
        LogUtil.e(TAG, "Failed to create temp file " + file.getAbsolutePath());
    }
    return uri;
}

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 w  w  w. j  a  va  2s .  com*/
    }
    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

/**
 * Try to return the absolute file path from the given Uri
 * /*from   ww w.  j ava 2s . co m*/
 * @param context
 * @param uri
 * @return the file path or null
 */
public static String getRealFilePath(final Context context, final Uri uri) {

    if (null == uri)
        return null;

    final String scheme = uri.getScheme();
    String data = null;

    if (scheme == null)
        data = uri.getPath();
    else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        data = uri.getPath();
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        Cursor cursor = context.getContentResolver().query(uri, new String[] { ImageColumns.DATA }, null, null,
                null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}

From source file:Main.java

private static int getRotationFromCamera(Context context, Uri imageFile) {
    int rotate = 0;
    try {// ww w. j  av a 2s.c  o  m

        context.getContentResolver().notifyChange(imageFile, null);
        ExifInterface exif = new ExifInterface(imageFile.getPath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}

From source file:Main.java

@SuppressLint("NewApi")
public static String uriToPath(Context activity, Uri uri) {
    if (null == uri) {
        return null;
    }//from  w ww  .  j a  v a  2  s .  c o  m
    String urlStr = uri.toString();
    if (urlStr.startsWith("file://")) {
        return uri.getPath();
    }
    Cursor cursor = null;
    String idWhere;
    String id;
    String[] columns = { MediaStore.Images.Media.DATA };
    try {
        if (Build.VERSION.SDK_INT == 19 && DocumentsContract.isDocumentUri(activity, uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            id = split[1];
            idWhere = MediaStore.Images.Media._ID + "=?";
            cursor = activity.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                    idWhere, new String[] { id }, null);
        } else {
            cursor = activity.getContentResolver().query(uri, columns, null, null, null);
        }
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        }
    } catch (Exception e) {
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}