Java tutorial
//package com.java2s; //License from project: Open Source License import android.content.Context; import android.net.Uri; import android.support.annotation.Nullable; import android.util.Log; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; public class Main { private static final String TAG = "BitmapUtils"; @Nullable private static InputStream getStreamFromUri(Uri selectedImageURI, Context theContext) throws IOException { switch (selectedImageURI.getScheme()) { case "content": return theContext.getContentResolver().openInputStream(selectedImageURI); case "file": return new FileInputStream(new File(URI.create(selectedImageURI.toString()))); case "http": // Fall through case "https": final URL url = new URL(selectedImageURI.toString()); final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); return connection.getInputStream(); default: Log.w(TAG, "getStreamFromUri(): unsupported Uri scheme: " + selectedImageURI.getScheme()); return null; } } }