Monday, September 28, 2015

android - How to convert a bitmap to a drawable

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#ddebbe"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Convert Bitmap To Drawable"
        />
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.ActionBar;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import java.io.InputStream;
import android.graphics.BitmapFactory;
import java.io.IOException;


public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        final Button btn = (Button) findViewById(R.id.btn);
        final ImageView iv = (ImageView) findViewById(R.id.iv);

        final ActionBar ab = getActionBar();
        ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFEB1491")));

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Try to convert an asset image to a drawable object
                try{
                    InputStream is = getAssets().open("animals.png");
                    Bitmap bitmap = BitmapFactory.decodeStream(is);
                    Drawable drawable = ConvertBitmapToDrawable(bitmap);

                    // Set the ImageView image from drawable
                    iv.setImageDrawable(drawable);
                } catch(IOException e) // Catch the exception
                {
                    e.printStackTrace();
                }

               // Notify the user that bitmap converted to drawable
                Toast.makeText(getApplicationContext(), "Drawable created.",Toast.LENGTH_SHORT).show();
            }
        });
    }

    // Custom method to convert bitmap to drawable
    public Drawable ConvertBitmapToDrawable(Bitmap bitmap)
    {
        /*
            Drawable
                A Drawable is a general abstraction for "something that can be drawn."
                Most often you will deal with Drawable as the type of resource retrieved
                for drawing things to the screen; the Drawable class provides a generic
                API for dealing with an underlying visual resource that may take a variety
                of forms. Unlike a View, a Drawable does not have any facility
                to receive events or otherwise interact with the user.
        */

        /*
            BitmapDrawable
                A Drawable that wraps a bitmap and can be tiled, stretched, or aligned.
                You can create a BitmapDrawable from a file path, an input stream,
                through XML inflation, or from a Bitmap object.
        */

        /*
            public abstract Resources getResources ()
                Return a Resources instance for your application's package.
        */

        Drawable drawable = new BitmapDrawable(getResources(),bitmap);
        return drawable;
    }
}

More android examples