Java tutorial
//package com.java2s; /* This file is part of Subsonic. Subsonic is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Subsonic is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Subsonic. If not, see <http://www.gnu.org/licenses/>. Copyright 2009 (C) Sindre Mehus */ import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import java.lang.reflect.Constructor; public class Main { public static Drawable createDrawableFromBitmap(Context context, Bitmap bitmap) { // BitmapDrawable(Resources, Bitmap) was introduced in Android 1.6. Use reflection to maintain // compatibility with 1.5. try { Constructor<BitmapDrawable> constructor = BitmapDrawable.class.getConstructor(Resources.class, Bitmap.class); return constructor.newInstance(context.getResources(), bitmap); } catch (Throwable x) { return new BitmapDrawable(bitmap); } } }