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: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 w ww .j  a  v  a2  s.  c  o  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: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 a v  a 2 s.co m
 * @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:com.nextgis.mobile.map.Layer.java

protected static String getFileNameByUri(final Context context, Uri uri, String defaultName) {
    String fileName = defaultName;
    Uri filePathUri = uri;/*from w ww  .  j  ava2s.c o  m*/
    try {
        if (uri.getScheme().toString().compareTo("content") == 0) {
            Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
            if (cursor.moveToFirst()) {
                int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                //Instead of "MediaStore.Images.Media.DATA" can be used "_data"
                filePathUri = Uri.parse(cursor.getString(column_index));
                fileName = filePathUri.getLastPathSegment().toString();
            }
        } else if (uri.getScheme().compareTo("file") == 0) {
            fileName = filePathUri.getLastPathSegment().toString();
        } else {
            fileName = fileName + "_" + filePathUri.getLastPathSegment();
        }
    } catch (Exception e) {
        //do nothing, only return default file name;
        Log.d(TAG, e.getLocalizedMessage());
    }
    return fileName;
}

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();// w  ww .j  a  v  a2s .co m
    if (mediaUri.getQuery() != null)
        netUrl += "?" + mediaUri.getEncodedQuery();
    return new URL(netUrl);
}

From source file:com.example.app_2.utils.Utils.java

public static String getPath(Context context, Uri uri) {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;//from  w ww .j  ava2s.  com

        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) {
            e.printStackTrace();
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:Main.java

/**
 * Get a file path from a Uri./*from ww w .j  a  va 2  s .  c  o  m*/
 * 
 * @param context
 * @param uri
 * @return
 * @throws URISyntaxException
 * 
 * @author paulburke
 */
public static String getPath(Context context, Uri uri) throws URISyntaxException {

    if (DEBUG)
        Log.d(TAG + " File -",
                "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: "
                        + uri.getPort() + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme()
                        + ", Host: " + uri.getHost() + ", Segments: " + uri.getPathSegments().toString());

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        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;
}

From source file:id.nci.stm_9.FileHelper.java

/**
 * Get a file path from a Uri./*from   w  w  w  .j a  va2s . c o m*/
 * 
 * from https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/
 * afilechooser/utils/FileUtils.java
 * 
 * @param context
 * @param uri
 * @return
 * 
 * @author paulburke
 */
public static String getPath(Context context, Uri uri) {
    Log.d("Stm-9" + " File -",
            "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: " + uri.getPort()
                    + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme() + ", Host: " + uri.getHost()
                    + ", Segments: " + uri.getPathSegments().toString());

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        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;
}

From source file:com.entertailion.android.launcher.spotlight.ProcessSpotlight.java

/**
 * Process the spotlight JSON data// w w w  .j  a va2 s.c o  m
 * 
 * @param jsonFeed
 * @return
 * @throws JSONException
 */
public static List<SpotlightInfo> process(Context context, String jsonFeed) throws JSONException {
    JSONObject jsonObj = new JSONObject(jsonFeed);
    List<SpotlightInfo> result = null;
    if (null != jsonObj) {
        JSONArray models = (JSONArray) jsonObj.get("models");
        if (null != models && models.length() > 0) {
            result = new ArrayList<SpotlightInfo>();
            int position = 0;
            for (int i = 0; i < models.length(); i++) {
                JSONObject model = (JSONObject) models.get(i);
                String title = model.getString("title");
                String url = model.getString("url");
                String logo = model.getString("hdpiLogo");
                String icon = null;
                if (null == SpotlightInfo.spotlightIconMap.get(title.toLowerCase())) {
                    // icon file path
                    try {
                        icon = "spotlight_" + sanitizeName(title) + ".png";
                        File file = context.getFileStreamPath(icon);
                        if (!file.exists()) {
                            Uri uri = Uri.parse(url);
                            String alternateLogo = Utils.getWebSiteIcon(context,
                                    uri.getScheme() + "://" + uri.getHost());
                            if (alternateLogo != null && alternateLogo.trim().length() > 0) {
                                icon = alternateLogo;
                            } else {
                                // create a file-based icon from original
                                // 360x203
                                Bitmap bitmap = Utils.getBitmapFromURL(logo);
                                if (bitmap != null) {
                                    bitmap = Utils.crop(bitmap, 30, 30);
                                    Utils.saveToFile(context, bitmap, 100, 100, icon);
                                    bitmap.recycle();
                                    bitmap = null;
                                } else {
                                    icon = null;
                                }
                            }
                        }
                    } catch (Exception e) {
                        Log.e(LOG_TAG, "create spotlight icon", e);
                    }
                }
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                result.add(new SpotlightInfo(position++, title, browserIntent, logo, icon));
            }
        }
    }
    return result;
}

From source file:net.wequick.small.Small.java

public static Intent getIntentOfUri(Uri uri, Context context) {
    // System url schemes
    if (!uri.getScheme().equals("http") && !uri.getScheme().equals("https") && !uri.getScheme().equals("file")
            && ApplicationUtils.canOpenUri(uri, context)) {
        return ApplicationUtils.getIntentOfUri(uri);
    }//from w  w w  .j  a v a  2s . c  o m

    // Small url schemes
    Bundle bundle = Bundle.getLaunchableBundle(uri);
    if (bundle != null) {
        return bundle.createIntent(context);
    }
    return null;
}

From source file:de.spiritcroc.ownlog.FileHelper.java

/**
 * @param requestCode//  ww w  . ja  v  a 2  s  . c  o m
 * The request code ID that should be checked for in onRequestPermissionsResult by the caller
 * @return
 * True if all permissions already granted, false if they have to get granted first
 */
public static boolean checkFileUriReadPermissions(Activity activity, Uri uri, int requestCode) {
    boolean isStoragePermissionRequired = "file".equals(uri.getScheme());
    if (isStoragePermissionRequired && ContextCompat.checkSelfPermission(activity,
            Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.READ_EXTERNAL_STORAGE },
                requestCode);
        return false;
    } else {
        return true;
    }
}