Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get bitmap information from bitmap stored in drawable folder

I wanna read in a bitmap that I have in my drawable folder and store it as a Bitmap variable so that I can set it as a background. Would the best way to do this be using a "file reader"? like

  Bitmap decodeFile (String pathName) method

Or is there a way to just set it like this:

  Bitmap bmp = R.drawable."bitmapFileName"; 

(I have tried this but returns an int, just wondering if I was on the right track)

Any help would be great :)

like image 735
Ian Avatar asked Aug 30 '11 11:08

Ian


People also ask

What is a bitmap drawable?

A Bitmap is a representation of a bitmap image (something like java. awt. Image). A Drawable is an abstraction of "something that can be drawn".

What is bitmap drawable in android?

A bitmap is simply a rectangle of pixels. Each pixel can be set to a given color but exactly what color depends on the type of the pixel. The first two parameters give the width and the height in pixels. The third parameter specifies the type of pixel you want to use.

What is difference between Mipmap and drawable in android?

The mipmap folders are for placing your app/launcher icons (which are shown on the homescreen) in only. Any other drawable assets you use should be placed in the relevant drawable folders as before.


3 Answers

The R.drawable."bitmapFileName" is, indeed, just an integer, for it is an index (static integer) at your project's R class (see more here). You can load your bitmap from the resources's folder like this:

Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.yourBitmap);

I found this code at the Android Development Community.

like image 52
Alesqui Avatar answered Oct 16 '22 20:10

Alesqui


I usually use the assets folder

InputStream is = parentActivity.getResources().getAssets().open(iconFile);
Bitmap bmp = BitmapFactory.decodeStream(is);
BitmapDrawable bitmapDrawable = new BitmapDrawable(is);

then just yourView.setBackgroundDrawable(bitmapDrawable);

like image 39
mihail Avatar answered Oct 16 '22 21:10

mihail


It is possible to load a drawable or bitmap by name. Here is an example:

public Drawable getImageByName(String nameOfTheDrawable, Activity a){
    Drawable drawFromPath;
    int path = a.getResources().getIdentifier(nameOfTheDrawable, 
                                    "drawable", "com.mycompany.myapp"); 

    Options options = new BitmapFactory.Options();
    options.inScaled = false;
    Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);

    drawFromPath = new BitmapDrawable(source);  

    return drawFromPath;
}

You can of course return Bitmap instead a drawable.

Drawbale d = getImageByName("mageFileName", this);

like image 4
Lumis Avatar answered Oct 16 '22 20:10

Lumis