Receive bitmap information from a bitmap stored in a folder with the ability to transfer

I want to read in the bitmap that I have in my folder and store it as a Bitmap variable so that I can set it as a background. Would there be a better way to do this using "read files"? as

Bitmap decodeFile (String pathName) method 

Or is there a way to simply install it like this:

  Bitmap bmp = R.drawable."bitmapFileName"; 

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

Any help would be great :)

+7
source share
4 answers

R.drawable."bitmapFileName" is indeed an integer since it is an index (static integer) in your class R class (see more here ). You can load the bitmap from the resource folder as follows:

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

I found this code in the Android Developer Community .

+25
source

I usually use the resource folder

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

then just yourView.setBackgroundDrawable(bitmapDrawable);

+5
source

You can load a drawing 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 a Bitmap, rather than drawable.

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

+3
source
 /* * You can get Bitmap from drawable resource id in the following way */ BitmapDrawable drawable = (BitmapDrawable)context.getResources() .getDrawable(drawableId); bitmap = drawable.getBitmap(); 
0
source

All Articles