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 InputStream openInputStream(Context context, Uri uri) {
    if (null == uri)
        return null;
    String scheme = uri.getScheme();
    InputStream stream = null;//from  w  ww  . j  a v  a  2s .com
    if ((scheme == null) || ("file".equals(scheme))) {
        stream = openFileInputStream(uri.getPath());
    } else if ("content".equals(scheme)) {
        stream = openContentInputStream(context, uri);
    } else if (("http".equals(scheme)) || ("https".equals(scheme))) {
        stream = openRemoteInputStream(uri);
    }
    return stream;
}

From source file:com.whiuk.philip.opensmime.PathConverter.java

private static FileInformation handleFileScheme(Context context, Uri uri) {
    FileInputStream fileInputStream = null;
    final String filePath = uri.getPath();
    try {//from   w  w  w  . ja  va  2  s  . c  om
        File srcFile = new File(filePath);
        fileInputStream = new FileInputStream(srcFile);
        File tmpFile = copyToTempFile(context, fileInputStream);
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String mimeType = fileNameMap.getContentTypeFor(filePath);
        String fileName = FilenameUtils.getName(filePath);

        return new FileInformation(tmpFile, fileName, mimeType);
    } catch (IOException e) {
        Log.e(OpenSMIME.LOG_TAG, "error acquiring FileInforamtion in handleFileScheme", e);
    }

    return null;
}

From source file:de.fau.cs.mad.smile.android.encryption.PathConverter.java

private static FileInformation handleFileScheme(Context context, Uri uri) {
    FileInputStream fileInputStream = null;
    final String filePath = uri.getPath();
    try {//  w w w  .  j ava  2  s. co m
        File srcFile = new File(filePath);
        fileInputStream = new FileInputStream(srcFile);
        File tmpFile = copyToTempFile(context, fileInputStream);
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String mimeType = fileNameMap.getContentTypeFor(filePath);
        String fileName = FilenameUtils.getName(filePath);

        return new FileInformation(tmpFile, fileName, mimeType);
    } catch (IOException e) {
        Log.e(SMileCrypto.LOG_TAG, "error acquiring FileInforamtion in handleFileScheme", e);
    }

    return null;
}

From source file:Main.java

public static String getRealFilePath(final Context context, final Uri uri) {
    if (null == uri)
        return null;
    final String scheme = uri.getScheme();
    String data = null;/*  ww  w .  j av  a 2 s . c  om*/
    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[] { MediaStore.Images.ImageColumns.DATA }, null, null, null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}

From source file:Main.java

@TargetApi(19)
public static File getFromMediaUri(Context context, 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())) {
        String filePath = "";
        if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;/*from   ww  w . java2  s .co  m*/
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            }
            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            Cursor cursor = null;
            final String column = "_data";
            final String[] projection = { column };

            try {
                cursor = context.getContentResolver().query(contentUri, projection, selection, selectionArgs,
                        null);
                if (cursor != null && cursor.moveToFirst()) {
                    final int column_index = cursor.getColumnIndexOrThrow(column);
                    filePath = cursor.getString(column_index);
                }
            } finally {
                if (cursor != null)
                    cursor.close();
            }
        } else {
            final String[] filePathColumn = { MediaStore.MediaColumns.DATA,
                    MediaStore.MediaColumns.DISPLAY_NAME };
            Cursor cursor = null;
            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 images on API 13+
                    if (columnIndex != -1) {
                        filePath = cursor.getString(columnIndex);
                    }
                }
            } catch (IllegalArgumentException e) {
                // Google Drive images
                return getFromMediaUriPfd(context, resolver, uri);
            } catch (SecurityException ignored) {
                // Nothing we can do
            } finally {
                if (cursor != null)
                    cursor.close();
            }
        }

        if (!TextUtils.isEmpty(filePath)) {
            return new File(filePath);
        }
    }
    return null;
}

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 )
 *
 * @param context//from w  w  w  .j  a va 2 s .  c o  m
 * @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);
    }
    return stream;
}

From source file:com.callrecorder.android.FileHelper.java

public static DocumentFile getStorageFile(Context context) {
    Uri uri = UserPreferences.getStorageUri();
    String scheme = uri.getScheme();
    if (scheme == null || scheme.equals("file")) {
        return DocumentFile.fromFile(new File(uri.getPath()));
    } else {//w w  w .java  2  s.  c  o  m
        return DocumentFile.fromTreeUri(context, uri);
    }
}

From source file:com.callrecorder.android.FileHelper.java

public static Uri getContentUri(Context context, Uri uri) {
    if (uri.getScheme() == "content")
        return uri;
    return FileProvider.getUriForFile(context, "com.callrecorder.android.fileprovider",
            new File(uri.getPath()));
}

From source file:Main.java

/**
 * Try to get the exif orientation of the passed image uri
 * //from   w ww  .j  a v a2 s.c o m
 * @param context
 * @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[] { Images.ImageColumns.ORIENTATION, Images.ImageColumns.DATA }, null, null,
                        null);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }

            if (result == null) {
                return 0;
            }

            int orientationColumnIndex = result.getColumnIndex(Images.ImageColumns.ORIENTATION);
            int dataColumnIndex = result.getColumnIndex(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:de.petermoesenthin.alarming.util.FileUtil.java

/**
 * Returns the filename from a given path and replaces all spaces with underscores
 *
 * @param uri/*from   w  ww . jav  a  2 s .  co m*/
 * @return
 */
public static String getFilenameFromUriNoSpace(Uri uri) {
    String result = null;
    String path = uri.getPath();
    File f = getFile(path);
    result = f.getName().replaceAll("\\s", "_");
    return result;
}