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

/**
 * Try to get the exif orientation of the passed image uri
 *
 * @param context// w  w  w. j a  v a 2 s .c om
 * @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:Main.java

/**
 * Return an {@link InputStream} from the given uri. ( can be a local content, a file path or an http url )
 * //w ww.  j  av  a 2 s . c o  m
 * @param context
 * @param uri
 * @return the {@link InputStream} from the given uri, null if uri cannot be opened
 */
public static InputStream openInputStream(Context context, Uri uri) {
    if (null == uri)
        return null;
    final String scheme = uri.getScheme();
    InputStream stream = null;
    if (scheme == null || ContentResolver.SCHEME_FILE.equals(scheme)) {
        // from file
        stream = openFileInputStream(uri.getPath());
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        // from content
        stream = openContentInputStream(context, uri);
    } else if ("http".equals(scheme) || "https".equals(scheme)) {
        // from remote uri
        stream = openRemoteInputStream(uri);
    }
    return stream;
}

From source file:Main.java

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

    String path = null;/*  w  w w .j  a  v  a2 s. 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.oakesville.mythling.util.MediaStreamProxy.java

public static URL getNetUrl(Uri mediaUri) throws MalformedURLException {
    String netUrl = mediaUri.getScheme() + "://" + mediaUri.getHost() + ":" + mediaUri.getPort();
    netUrl += mediaUri.getPath();
    if (mediaUri.getQuery() != null)
        netUrl += "?" + mediaUri.getEncodedQuery();
    return new URL(netUrl);
}

From source file:ca.rmen.android.networkmonitor.app.dbops.ui.Import.java

/**
 * Import a database file.// w  w w  .jav a  2s . c om
 *
 * @param activity A progress dialog will appear on the activity during the import
 * @param uri the location of the db to import
 */
public static void importDb(final FragmentActivity activity, final Uri uri) {
    Log.v(TAG, "importDb: uri = " + uri);
    DBImport dbImport = new DBImport(activity, uri);
    DBOpAsyncTask<Boolean> task = new DBOpAsyncTask<Boolean>(activity, dbImport, null) {

        @Override
        protected void onPostExecute(Boolean result) {
            String toastText = result ? activity.getString(R.string.import_successful, uri.getPath())
                    : activity.getString(R.string.import_failed, uri.getPath());
            Toast.makeText(activity, toastText, Toast.LENGTH_SHORT).show();
            super.onPostExecute(result);
        }
    };
    task.execute();

}

From source file:Main.java

/**
 * Calculates the amount of rotation needed for the image to look upright.
 * /*www  .j  av a 2s  .  c  om*/
 * @param context
 *            the application's context
 * @param uri
 *            the Uri pointing to the file
 * @return the needed rotation as a <code>float</code>
 */
private static float rotationForImage(Context context, Uri uri) {
    if ("content".equals(uri.getScheme())) {
        String[] projection = { Images.ImageColumns.ORIENTATION };
        Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
        if (c.moveToFirst()) {
            return c.getInt(0);
        }
    } else if ("file".equals(uri.getScheme())) {
        try {
            ExifInterface exif = new ExifInterface(uri.getPath());
            int rotation = (int) exifOrientationToDegrees(
                    exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
            return rotation;
        } catch (IOException e) {
            Log.e(TAG, "Error checking exif", e);
        }
    }
    return 0f;
}

From source file:com.cyanogenmod.filemanager.util.MediaHelper.java

/**
 * Method that converts a content uri to a file system path
 *
 * @param cr The content resolver/*from  w  ww .j  av a  2s .  c om*/
 * @param uri The content uri
 * @return File The file reference
 */
public static File contentUriToFile(ContentResolver cr, Uri uri) {
    // Sanity checks
    if (uri == null || uri.getScheme() == null || uri.getScheme().compareTo("content") != 0) {
        return null;
    }

    // Retrieve the request id
    long id;
    try {
        id = Long.parseLong(new File(uri.getPath()).getName());
    } catch (NumberFormatException nfex) {
        return null;
    }

    // Check in external and internal storages
    File file = mediaIdToFile(cr, id, EXTERNAL_VOLUME);
    if (file != null) {
        return file;
    }
    file = mediaIdToFile(cr, id, INTERNAL_VOLUME);
    if (file != null) {
        return file;
    }
    return null;
}

From source file:Main.java

/**
 * Utils to get File path/* w ww.  j a va  2  s .  c  om*/
 * 
 * @param uri
 * @return
 */
public static String getPath(Context context, Uri uri) {
    String scheme = uri.getScheme();
    String s = null;
    if (scheme.equals("content")) {
        String[] projection = { MediaStore.Files.FileColumns.DATA };
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
        cursor.moveToFirst();
        s = cursor.getString(columnIndex);
    } else if (scheme.equals("file")) {
        s = uri.getPath();
    }
    // Log.d("ActionManager", "URI:" + uri + " - S:" + s);
    return s;
}

From source file:Main.java

/** @see http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue */
public static int getSampleSize(Context context, Uri uri, float maxSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*w  w w.  jav  a2  s  . c o  m*/
    if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
        try {
            InputStream stream = context.getContentResolver().openInputStream(uri);
            BitmapFactory.decodeStream(stream, null, options);
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else
        BitmapFactory.decodeFile(uri.getPath(), options);
    int longSide = Math.max(options.outHeight, options.outWidth);
    return getSampleSize(longSide, maxSize);
}

From source file:Main.java

public static File uri2File(Activity context, Uri uri) {
    File file;/*w  ww .  jav  a  2  s  .com*/
    String[] project = { MediaStore.Images.Media.DATA };
    Cursor actualImageCursor = context.getContentResolver().query(uri, project, null, null, null);
    if (actualImageCursor != null) {
        int actual_image_column_index = actualImageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        actualImageCursor.moveToFirst();
        String img_path = actualImageCursor.getString(actual_image_column_index);
        file = new File(img_path);
    } else {
        file = new File(uri.getPath());
    }
    if (actualImageCursor != null)
        actualImageCursor.close();
    return file;
}