List of usage examples for android.net Uri getScheme
@Nullable public abstract String getScheme();
From source file:de.wikilab.android.friendica01.Max.java
public static String getRealPathFromURI(Context ctx, Uri contentUri) { Log.i("FileTypes", "URI = " + contentUri + ", scheme = " + contentUri.getScheme()); if (contentUri.getScheme().equals("file")) { return contentUri.getPath(); } else {// w ww. j a va 2 s . c o m String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = ctx.getContentResolver().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String res = cursor.getString(column_index); cursor.close(); return res; } }
From source file:Main.java
/** * Get a file path from a Uri. This will get the the path for Storage Access * Framework Documents, as well as the _data field for the MediaStore and * other file-based ContentProviders.<br> * <br>//from w w w. ja va2 s .c om * Callers should check whether the path is local before assuming it * represents a local file. * * @param context The context. * @param uri The Uri to query. * @author paulburke * @see #isLocal(String) * @see #getFile(Context, Uri) */ @SuppressLint("NewApi") public static String getPath(final Context context, final Uri uri) { 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()); final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } // TODO handle non-primary volumes } // DownloadsProvider else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] }; return getDataColumn(context, contentUri, selection, selectionArgs); } } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) { // Return the remote address if (isGooglePhotosUri(uri)) return uri.getLastPathSegment(); return getDataColumn(context, uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; }
From source file:com.google.android.apps.muzei.util.IOUtil.java
public static InputStream openUri(Context context, Uri uri, String reqContentTypeSubstring) throws OpenUriException { if (uri == null) { throw new IllegalArgumentException("Uri cannot be empty"); }/*from www .java 2s . c om*/ String scheme = uri.getScheme(); InputStream in = null; if ("content".equals(scheme)) { try { in = context.getContentResolver().openInputStream(uri); } catch (FileNotFoundException e) { throw new OpenUriException(false, e); } catch (SecurityException e) { throw new OpenUriException(false, e); } } else if ("file".equals(scheme)) { List<String> segments = uri.getPathSegments(); if (segments != null && segments.size() > 1 && "android_asset".equals(segments.get(0))) { AssetManager assetManager = context.getAssets(); StringBuilder assetPath = new StringBuilder(); for (int i = 1; i < segments.size(); i++) { if (i > 1) { assetPath.append("/"); } assetPath.append(segments.get(i)); } try { in = assetManager.open(assetPath.toString()); } catch (IOException e) { throw new OpenUriException(false, e); } } else { try { in = new FileInputStream(new File(uri.getPath())); } catch (FileNotFoundException e) { throw new OpenUriException(false, e); } } } else if ("http".equals(scheme) || "https".equals(scheme)) { OkHttpClient client = new OkHttpClient(); HttpURLConnection conn = null; int responseCode = 0; String responseMessage = null; try { conn = client.open(new URL(uri.toString())); conn.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT); conn.setReadTimeout(DEFAULT_READ_TIMEOUT); responseCode = conn.getResponseCode(); responseMessage = conn.getResponseMessage(); if (!(responseCode >= 200 && responseCode < 300)) { throw new IOException("HTTP error response."); } if (reqContentTypeSubstring != null) { String contentType = conn.getContentType(); if (contentType == null || contentType.indexOf(reqContentTypeSubstring) < 0) { throw new IOException("HTTP content type '" + contentType + "' didn't match '" + reqContentTypeSubstring + "'."); } } in = conn.getInputStream(); } catch (MalformedURLException e) { throw new OpenUriException(false, e); } catch (IOException e) { if (conn != null && responseCode > 0) { throw new OpenUriException(500 <= responseCode && responseCode < 600, responseMessage, e); } else { throw new OpenUriException(false, e); } } } return in; }
From source file:com.androidzeitgeist.dashwatch.common.IOUtil.java
public static InputStream openUri(Context context, Uri uri, String reqContentTypeSubstring) throws OpenUriException { if (uri == null) { throw new IllegalArgumentException("Uri cannot be empty"); }/*w w w . j av a 2 s . c om*/ String scheme = uri.getScheme(); if (scheme == null) { throw new OpenUriException(false, new IOException("Uri had no scheme")); } InputStream in = null; if ("content".equals(scheme)) { try { in = context.getContentResolver().openInputStream(uri); } catch (FileNotFoundException e) { throw new OpenUriException(false, e); } catch (SecurityException e) { throw new OpenUriException(false, e); } } else if ("file".equals(scheme)) { List<String> segments = uri.getPathSegments(); if (segments != null && segments.size() > 1 && "android_asset".equals(segments.get(0))) { AssetManager assetManager = context.getAssets(); StringBuilder assetPath = new StringBuilder(); for (int i = 1; i < segments.size(); i++) { if (i > 1) { assetPath.append("/"); } assetPath.append(segments.get(i)); } try { in = assetManager.open(assetPath.toString()); } catch (IOException e) { throw new OpenUriException(false, e); } } else { try { in = new FileInputStream(new File(uri.getPath())); } catch (FileNotFoundException e) { throw new OpenUriException(false, e); } } } else if ("http".equals(scheme) || "https".equals(scheme)) { OkHttpClient client = new OkHttpClient(); HttpURLConnection conn = null; int responseCode = 0; String responseMessage = null; try { conn = client.open(new URL(uri.toString())); conn.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT); conn.setReadTimeout(DEFAULT_READ_TIMEOUT); responseCode = conn.getResponseCode(); responseMessage = conn.getResponseMessage(); if (!(responseCode >= 200 && responseCode < 300)) { throw new IOException("HTTP error response."); } if (reqContentTypeSubstring != null) { String contentType = conn.getContentType(); if (contentType == null || contentType.indexOf(reqContentTypeSubstring) < 0) { throw new IOException("HTTP content type '" + contentType + "' didn't match '" + reqContentTypeSubstring + "'."); } } in = conn.getInputStream(); } catch (MalformedURLException e) { throw new OpenUriException(false, e); } catch (IOException e) { if (conn != null && responseCode > 0) { throw new OpenUriException(500 <= responseCode && responseCode < 600, responseMessage, e); } else { throw new OpenUriException(false, e); } } } return in; }
From source file:edu.mit.mobile.android.locast.data.CastMedia.java
/** * @param context//from w w w . j ava2s . c o m * @param c * @param castMediaUri * */ public static void showMedia(Context context, Cursor c, Uri castMediaUri) { final String mediaString = c.getString(c.getColumnIndex(CastMedia._MEDIA_URL)); final String locMediaString = c.getString(c.getColumnIndex(CastMedia._LOCAL_URI)); String mimeType = null; Uri media; if (locMediaString != null) { media = Uri.parse(locMediaString); if ("file".equals(media.getScheme())) { mimeType = c.getString(c.getColumnIndex(CastMedia._MIME_TYPE)); } } else if (mediaString != null) { media = Uri.parse(mediaString); mimeType = c.getString(c.getColumnIndex(CastMedia._MIME_TYPE)); // we strip this because we don't really want to force them to go to the browser. if ("text/html".equals(mimeType)) { mimeType = null; } } else { Log.e(TAG, "asked to show media for " + castMediaUri + " but there was nothing to show"); return; } final Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(media, mimeType); if (mimeType != null && mimeType.startsWith("video/")) { context.startActivity(new Intent(Intent.ACTION_VIEW, ContentUris.withAppendedId(castMediaUri, c.getLong(c.getColumnIndex(CastMedia._ID))))); } else { // setting the MIME type for URLs doesn't work. try { context.startActivity(i); } catch (final ActivityNotFoundException e) { // try it again, but without a mime type. if (mimeType != null) { i.setDataAndType(media, null); } try { context.startActivity(i); } catch (final ActivityNotFoundException e2) { Toast.makeText(context, R.string.error_cast_media_no_activities, Toast.LENGTH_LONG).show(); } } } }
From source file:com.android.emailcommon.provider.HostAuth.java
public static String getProtocolFromString(String uriString) { final Uri uri = Uri.parse(uriString); final String scheme = uri.getScheme(); final String[] schemeParts = scheme.split("\\+"); return schemeParts[0]; }
From source file:de.vanita5.twittnuker.util.TwitterWrapper.java
public static void updateProfileBannerImage(final Context context, final Twitter twitter, final Uri imageUri, final boolean deleteImage) throws FileNotFoundException, TwitterException { InputStream is;/*ww w .j ava2 s . c om*/ try { is = context.getContentResolver().openInputStream(imageUri); twitter.updateProfileBannerImage(is); } finally { if (deleteImage && "file".equals(imageUri.getScheme())) { final File file = new File(imageUri.getPath()); if (!file.delete()) { Log.w(LOGTAG, String.format("Unable to delete %s", file)); } } } }
From source file:de.vanita5.twittnuker.util.TwitterWrapper.java
public static User updateProfileImage(final Context context, final Twitter twitter, final Uri imageUri, final boolean deleteImage) throws FileNotFoundException, TwitterException { InputStream is;/*from ww w .j av a 2 s . c o m*/ try { is = context.getContentResolver().openInputStream(imageUri); return twitter.updateProfileImage(is); } finally { if (deleteImage && "file".equals(imageUri.getScheme())) { final File file = new File(imageUri.getPath()); if (!file.delete()) { Log.w(LOGTAG, String.format("Unable to delete %s", file)); } } } }
From source file:com.karura.framework.plugins.Capture.java
/** * Queries the media store to find out what the file path is for the Uri we supply * //from w w w . j a v a 2 s. c o m * @param contentUri * the Uri of the audio/image/video * @param context * the current application context * @return the full path to the file */ @SuppressWarnings("deprecation") public static String getRealPathFromURI(Uri contentUri, Activity context) { final String scheme = contentUri.getScheme(); if (scheme == null) { return contentUri.toString(); } else if (scheme.compareTo("content") == 0) { String[] proj = { DATA }; Cursor cursor = context.managedQuery(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(DATA); cursor.moveToFirst(); return cursor.getString(column_index); } else if (scheme.compareTo("file") == 0) { return contentUri.getPath(); } else { return contentUri.toString(); } }
From source file:Main.java
@TargetApi(Build.VERSION_CODES.KITKAT) public static String getFilePath(final Context context, final Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { // ExternalStorageProvider if ("com.android.externalStorage.documents".equalsIgnoreCase(uri.getAuthority())) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type) || "content".equalsIgnoreCase(uri.getScheme())) { return Environment.getExternalStorageDirectory() + "/" + split[1]; }//www .ja va 2 s .c om } // DownloadsProvider else if ("com.android.providers.downloads.documents".equalsIgnoreCase(uri.getAuthority())) { final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); } // MediaProvider else if ("com.android.providers.media.documents".equalsIgnoreCase(uri.getAuthority())) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] }; return getDataColumn(context, contentUri, selection, selectionArgs); } } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) { return getDataColumn(context, uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return ""; }