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

public static Bitmap decodeSampledBitmapFromUri(Uri uri, int reqWidth, int reqHeight) {

    System.out.println("k9d3 sampling " + uri.getPath() + " for width=" + reqWidth + " height=" + reqHeight);
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from   w ww  .j a va2s . c o m
    BitmapFactory.decodeFile(uri.getPath(), options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(uri.getPath(), options);
}

From source file:Main.java

/**
 * Get {@link File} object for the given Android URI.<br>
 * Use content resolver to get real path if direct path doesn't return valid file.
 *//*from w w  w . j  a  v a 2  s. c o  m*/
private static File getFileFromUri(Context context, Uri uri) {

    // first try by direct path
    File file = new File(uri.getPath());
    if (file.exists()) {
        return file;
    }

    // try reading real path from content resolver (gallery images)
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            String realPath = cursor.getString(column_index);
            file = new File(realPath);
        }
    } catch (Exception ignored) {
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return file;
}

From source file:Main.java

public static boolean isPhotoReallyCropped(Uri uri) {
    File file = new File(uri.getPath());
    long length = file.length();
    return length > 0;
}

From source file:Main.java

public static String getRealPathByUri(Context context, Uri uri) {
    if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
        return uri.getPath();
    }//  ww  w  .ja  va  2 s . com

    try {
        ContentResolver resolver = context.getContentResolver();
        String[] proj = new String[] { MediaStore.Images.Media.DATA };
        Cursor cursor = MediaStore.Images.Media.query(resolver, uri, proj);
        String realPath = null;
        if (cursor != null) {
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.getCount() > 0 && cursor.moveToFirst()) {
                realPath = cursor.getString(columnIndex);
            }
            cursor.close();
        }
        return realPath;
    } catch (Exception e) {
        return uri.getPath();
    }
}

From source file:Main.java

public static boolean urlsMatchOnPath(String url1, String url2) {
    try {//from w w w .  jav a 2 s.c  om
        Uri uri1 = Uri.parse(url1);
        Uri uri2 = Uri.parse(url2);
        String path1 = uri1.getPath();
        String path2 = uri2.getPath();

        if (path1.length() >= 2 && path1.substring(0, 2).equals("//"))
            path1 = path1.substring(1, path1.length());
        if (path2.length() >= 2 && path2.substring(0, 2).equals("//"))
            path2 = path2.substring(1, path2.length());

        if (path1.isEmpty())
            path1 = "/";
        if (path2.isEmpty())
            path2 = "/";

        String host1 = uri1.getHost();
        String host2 = uri2.getHost();
        if (host1.startsWith("www."))
            host1 = host1.substring(4);
        if (host2.startsWith("www."))
            host2 = host2.substring(4);

        return host1.equals(host2) && path1.equals(path2);
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

/**
 * Deletes an image given its Uri./* w  w w  . ja v a 2  s .  co m*/
 *
 * @param cameraPicUri
 * @param context
 * @return true if it was deleted successfully, false otherwise.
 */
public static boolean deleteImageWithUriIfExists(Uri cameraPicUri, Context context) {
    if (cameraPicUri != null) {
        File fdelete = new File(cameraPicUri.getPath());
        if (fdelete.exists()) {
            if (fdelete.delete()) {
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                        Uri.parse("file://" + Environment.getExternalStorageDirectory())));
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

public static int getCameraPhotoOrientation(Uri imageUri) {
    int rotate = 0;
    try {/*from   ww  w  .  ja  v a  2s  .  com*/
        File imageFile = new File(imageUri.getPath());
        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
        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

public static BitmapDrawable getBitmapDrawable(Context context, Uri uri) {
    System.out.println("uri path = " + uri.getPath());
    Bitmap b;/*w  ww  .j av  a2 s.c o  m*/
    try {
        String path = uri.getPath();
        if (mCache.containsKey(path)) {
            BitmapDrawable d = mCache.get(path).get();
            if (d != null) {
                System.out.println("not recycle path = " + uri.getPath());
                return d;
            } else {
                System.out.println("be recycle path = " + uri.getPath());
                mCache.remove(path);
            }
        }
        System.out.println("----->safeDecodeStream start");
        b = safeDecodeStream(context, uri, 60, 60);
        System.out.println("----->safeDecodeStream end");
        final BitmapDrawable bd = new BitmapDrawable(b);
        mCache.put(path, new SoftReference<BitmapDrawable>(bd));
        return bd;
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Returns a title suitable for display for a link. If |title| is non-empty, this simply returns
 * it. Otherwise, returns a shortened form of the URL.
 *//* w  w  w .  j a  v a  2 s.co  m*/
static String getTitleForDisplay(@Nullable String title, @Nullable String url) {
    if (!TextUtils.isEmpty(title) || TextUtils.isEmpty(url)) {
        return title;
    }

    Uri uri = Uri.parse(url);
    String host = uri.getHost();
    if (host == null)
        host = "";
    String path = uri.getPath();
    if (path == null || path.equals("/"))
        path = "";
    title = host + path;
    return title;
}

From source file:Main.java

public static File getFileFromUri(Uri uri, Activity activity) {
    String filePath = null;//from ww  w. j a  v a  2 s . c  om
    String scheme = uri.getScheme();
    filePath = uri.getPath();
    if (filePath != null && scheme != null && scheme.equals("file")) {
        return new File(filePath);
    }

    String[] projection = { MediaStore.Images.ImageColumns.DATA };
    Cursor c = activity.managedQuery(uri, projection, null, null, null);
    if (c != null && c.moveToFirst()) {
        filePath = c.getString(0);
    }
    if (filePath != null) {
        return new File(filePath);
    }
    return null;

}