Code example for Uri

Methods: getPath, getScheme, toString

0
	 * @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;
	} 
 
	public static boolean decodeImageBounds( final InputStream stream, int[] outSize ) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeStream( stream, null, options );
		if ( options.outHeight > 0 && options.outWidth > 0 ) {
			outSize[0] = options.outWidth;
			outSize[1] = options.outHeight;
			return true; 
		} 
		return false; 
	} 
 
	private static int computeSampleSize( final int bitmapW, final int bitmapH, final int maxW, final int maxH,
			final int orientation ) {
		double w, h;
 
		if ( orientation == 0 || orientation == 180 ) {
			w = bitmapW;
			h = bitmapH;
		} else { 
			w = bitmapH;
			h = bitmapW;
		} 
 
		final int sampleSize = (int) Math.ceil( Math.max( w / maxW, h / maxH ) );
		return sampleSize;
	} 
 
	/** 
	 * Return a {@link FileInputStream} from the given path or null if file not found 
	 *  
	 * @param path 
	 *           the file path 
	 * @return the {@link FileInputStream} of the given path, null if {@link FileNotFoundException} is thrown 
	 */ 
	static InputStream openFileInputStream( String path ) {
		try { 
			return new FileInputStream( path );
		} catch ( FileNotFoundException e ) {
			e.printStackTrace();
		} 
		return null; 
	} 
 
	/** 
	 * Return a {@link BufferedInputStream} from the given uri or null if an exception is thrown 
	 *  
	 * @param context 
	 * @param uri 
	 * @return the {@link InputStream} of the given path. null if file is not found 
	 */ 
	static InputStream openContentInputStream( Context context, Uri uri ) {
		try { 
			return context.getContentResolver().openInputStream( uri );
		} catch ( FileNotFoundException e ) {
			e.printStackTrace();
		} 
		return null; 
	} 
 
	/** 
	 * Return an {@link InputStream} from the given url or null if failed to retrieve the content 
	 *  
	 * @param uri 
	 * @return 
	 */ 
	static InputStream openRemoteInputStream( Uri uri ) {
		java.net.URL finalUrl;
		try { 
			finalUrl = new java.net.URL( uri.toString() );
		} catch ( MalformedURLException e ) {
			e.printStackTrace();
			return null; 
		}