Example usage for android.net Uri getScheme

List of usage examples for android.net Uri getScheme

Introduction

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

Prototype

@Nullable
public abstract String getScheme();

Source Link

Document

Gets the scheme of this URI.

Usage

From source file:edu.mit.media.funf.util.IOUtil.java

public static boolean isValidUrl(String url) {
    Log.d(LogUtil.TAG, "Validating url");
    boolean isValidUrl = false;
    if (url != null && !url.trim().equals("")) {
        try {/*from ww w  .  j  a va2s .  co  m*/
            Uri test = Uri.parse(url);
            isValidUrl = test.getScheme() != null && test.getScheme().startsWith("http")
                    && test.getHost() != null && !test.getHost().trim().equals("");
        } catch (Exception e) {
            Log.d(LogUtil.TAG, "Not valid", e);
        }
    }
    Log.d(LogUtil.TAG, "Valid url? " + isValidUrl);
    return isValidUrl;
}

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

public static int defineExifOrientation(Uri imageUri, String mimeType) {
    int rotation = 0;
    if ("image/jpeg".equalsIgnoreCase(mimeType) && "file".equals(imageUri.getScheme())) {
        try {//ww w .ja v a  2s  . c om
            ExifInterface exif = new ExifInterface(imageUri.getPath());
            int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            Log.e("dsd", "exifOrientation:" + exifOrientation);
            switch (exifOrientation) {
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            case ExifInterface.ORIENTATION_NORMAL:
                rotation = 0;
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotation = 90;
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotation = 180;
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotation = 270;
                break;
            }
        } catch (IOException e) {
            Log.e("dsd", "Can't read EXIF tags from file [%s]" + imageUri);
        }
    }
    return rotation;
}

From source file:Main.java

public static File getFile(Context context, Uri uri, boolean forceCreation) {

    if (!forceCreation && "file".equalsIgnoreCase(uri.getScheme())) {
        return new File(uri.getPath());
    }//from ww w.  ja  v a 2  s . c  o m

    File file = null;

    try {
        File root = context.getFilesDir();
        if (root == null) {
            throw new Exception("data dir not found");
        }
        file = new File(root, getFilename(context, uri));
        file.delete();
        InputStream is = context.getContentResolver().openInputStream(uri);
        OutputStream os = new FileOutputStream(file);
        byte[] buf = new byte[1024];
        int cnt = is.read(buf);
        while (cnt > 0) {
            os.write(buf, 0, cnt);
            cnt = is.read(buf);
        }
        os.close();
        is.close();
        file.deleteOnExit();
    } catch (Exception e) {
        Log.e("OpenFile", e.getMessage(), e);
    }
    return file;
}

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 )
 * /*from   w  w  w .j  a  v a2 s  .  com*/
 * @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 get the exif orientation of the passed image uri
 * /*from   w ww .j  ava  2  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:Main.java

/**
 * Try to get the exif orientation of the passed image uri
 *
 * @param context/*from w  w  w .j  a  v a2  s .  co m*/
 * @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

public static String extractFilenameFromUri(Uri uri, Activity activity) {

    String fileName = null;/*  w  ww.  j  ava2  s.  com*/
    if (uri == null) {
        throw new IllegalArgumentException();
    }
    String scheme = uri.getScheme();
    String path = uri.getPath();
    if (path != null && scheme != null && scheme.equals("file")) {
        fileName = path.substring(path.lastIndexOf("/") + 1);
    }

    String[] projection = { MediaStore.Images.ImageColumns.DISPLAY_NAME /* col1 */ };

    Cursor c = activity.managedQuery(uri, projection, null, null, null);
    if (c != null && c.moveToFirst()) {
        fileName = c.getString(0);
    }
    return fileName;
}

From source file:Main.java

@Nullable
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())) {
        final String[] filePathColumn = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME };
        Cursor cursor = null;/*w ww  .j  ava 2s  . com*/
        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) {
                    String filePath = cursor.getString(columnIndex);
                    if (!TextUtils.isEmpty(filePath)) {
                        return new File(filePath);
                    }
                }
            }
        } catch (IllegalArgumentException e) {
            // Google Drive images
            return getFromMediaUriPfd(context, resolver, uri);
        } catch (SecurityException ignored) {
            // Nothing we can do
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
    return null;
}

From source file:com.trigger_context.conf.Set_File_Select.java

public static String getPath(Context context, Uri uri) throws URISyntaxException {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;/*from  ww  w.j a  v  a  2s.co  m*/

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    }

    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}