Java tutorial
//package com.java2s; /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.util.Log; public class Main { private static final String LOGTAG = "GeckoBitmapUtils"; public static Bitmap decodeUrl(Uri uri) { return decodeUrl(uri.toString()); } public static Bitmap decodeUrl(String urlString) { URL url; try { url = new URL(urlString); } catch (MalformedURLException e) { Log.w(LOGTAG, "decodeUrl: malformed URL " + urlString); return null; } return decodeUrl(url); } public static Bitmap decodeUrl(URL url) { InputStream stream = null; try { stream = url.openStream(); } catch (IOException e) { Log.w(LOGTAG, "decodeUrl: IOException downloading " + url); return null; } if (stream == null) { Log.w(LOGTAG, "decodeUrl: stream not found downloading " + url); return null; } Bitmap bitmap = decodeStream(stream); try { stream.close(); } catch (IOException e) { Log.w(LOGTAG, "decodeUrl: IOException closing stream " + url, e); } return bitmap; } public static Bitmap decodeStream(InputStream inputStream) { try { return BitmapFactory.decodeStream(inputStream); } catch (OutOfMemoryError e) { Log.e(LOGTAG, "decodeStream() OOM!", e); return null; } } }