Example usage for android.content ContentResolver SCHEME_FILE

List of usage examples for android.content ContentResolver SCHEME_FILE

Introduction

In this page you can find the example usage for android.content ContentResolver SCHEME_FILE.

Prototype

String SCHEME_FILE

To view the source code for android.content ContentResolver SCHEME_FILE.

Click Source Link

Usage

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.  j av  a2  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 String getRealFilePath(Context context, Uri uri) {
    if (null == uri)
        return null;
    String scheme = uri.getScheme();
    String data = null;//from ww  w  . j  a v  a  2 s  . 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:Main.java

/**
 * Try to return the absolute file path from the given Uri
 * // w w  w . j  a va2s  .com
 * @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

/**
 * /*from w  w  w .  ja  v a 2  s  .  co  m*/
 * @param context
 * @param uri
 *            uri of SCHEME_FILE or SCHEME_CONTENT
 * @return image path; uri will be changed to SCHEME_FILE
 */
public static String uriToImagePath(Context context, Uri uri) {
    if (context == null || uri == null) {
        return null;
    }

    String imagePath = null;
    String uriString = uri.toString();
    String uriSchema = uri.getScheme();
    if (uriSchema.equals(ContentResolver.SCHEME_FILE)) {
        imagePath = uriString.substring("file://".length());
    } else {// uriSchema.equals(ContentResolver.SCHEME_CONTENT)
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(uri, null, null, null, null);
        if (cursor.getCount() == 0) {
            Log.e(TAG, "Uri(" + uri.toString() + ") not found!");
            return null;
        }
        cursor.moveToFirst();
        imagePath = cursor.getString(1);
        // Change the SCHEME_CONTENT uri to the SCHEME_FILE.
        uri = Uri.fromFile(new File(imagePath));
    }
    Log.v(TAG, "Final uri: " + uri.toString());
    return imagePath;
}

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;/*from   w  ww  .  j ava  2 s . co 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[] { 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

/**
 * Return an {@link InputStream} from the given uri. ( can be a local content, a file path or an http url )
 * //w  w  w.java2s .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

/**
 * Try to return the absolute file path from the given Uri
 *
 * @param context/*from  w ww  .j a v  a  2  s .  c o m*/
 * @param uri
 * @return the file path or null
 */
public static String uri2FilePath(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[] { 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

public static InputStream getInputStream(Context context, Uri uri) throws IOException {
    InputStream inputStream;/*from  w w w. j ava2 s  .  com*/
    if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme())
            || ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme())
            || ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
        inputStream = context.getContentResolver().openInputStream(uri);
    } else {
        URLConnection urlConnection = new URL(uri.toString()).openConnection();
        urlConnection.setConnectTimeout(URLCONNECTION_CONNECTION_TIMEOUT_MS);
        urlConnection.setReadTimeout(URLCONNECTION_READ_TIMEOUT_MS);
        inputStream = urlConnection.getInputStream();
    }
    return new BufferedInputStream(inputStream);
}

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  ww .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:org.sufficientlysecure.keychain.ui.util.PermissionsUtil.java

private static boolean checkReadPermission(Context context, Uri uri) {
    if (!ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
        return true;
    }//from w  ww  .ja v  a2 s. co  m

    // Additional check due to:
    // https://commonsware.com/blog/2015/11/09/you-cannot-hold-nonexistent-permissions.html
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return true;
    }

    if (ContextCompat.checkSelfPermission(context,
            Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        return true;
    }

    return false;
}