I have a code that let me use images in drawable folder, but all the images are stored in array by name-
I wanna load them programmatically by a function and store them in array
this is the array :
private int[] theBitmapIds = { R.drawable.elefant_001, R.drawable.elefant_002, R.drawable.elefant_003, R.drawable.elefant_004,
R.drawable.elefant_005, R.drawable.elefant_006, R.drawable.elefant_007,
R.drawable.elefant_008, R.drawable.elefant_009, R.drawable.elefant_010,
R.drawable.elefant_011, R.drawable.elefant_012, R.drawable.elefant_013,
R.drawable.elefant_014, R.drawable.elefant_015, R.drawable.elefant_016,
R.drawable.elefant_017, R.drawable.elefant_018, R.drawable.elefant_019,
R.drawable.elefant_020, R.drawable.elefant_021,R.drawable.elefant_022,
R.drawable.elefant_022, R.drawable.elefant_023,R.drawable.elefant_024,
R.drawable.elefant_025, R.drawable.elefant_026, R.drawable.elefant_027,
R.drawable.elefant_028, R.drawable.elefant_029, R.drawable.elefant_030,
R.drawable.elefant_031, R.drawable.elefant_032, R.drawable.elefant_033,
R.drawable.elefant_034, R.drawable.elefant_035, R.drawable.elefant_036,
R.drawable.elefant_037, R.drawable.elefant_038, R.drawable.elefant_039,
R.drawable.elefant_040};
I changed it by the function
public int [] theBitmapIds( ) {
Field[] ID_Fields = R.drawable.class.getFields();
int[] theBitmapIds = new int[ID_Fields.length];
for (int i = 0; i < ID_Fields.length; i++) {
try {
theBitmapIds[i] = ID_Fields[i].getInt( null );
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return theBitmapIds;
}
private int[] theBitmapIds=theBitmapIds( );
but when I call the array in other function, it still has nothing in it
public Bitmap getBitmap(int width, int height, int index) {
Bitmap b = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
b.eraseColor(0xFFFFFFFF);
Canvas c = new Canvas(b);
Drawable d = getResources().getDrawable(theBitmapIds[index]);
int margin = 1;
int border = 1;
Rect r = new Rect(margin, margin, width - margin, height - margin);
int imageWidth = r.width() - (border * 2);
int imageHeight = imageWidth * d.getIntrinsicHeight()
/ d.getIntrinsicWidth();
if (imageHeight > r.height() - (border * 2)) {
imageHeight = r.height() - (border * 2);
imageWidth = imageHeight * d.getIntrinsicWidth()
/ d.getIntrinsicHeight();
}
I am confused especially I'm a noob in java. How I can make this code work by editing the function or by change this line of code:
private int[] theBitmapIds=theBitmapIds( );
or
Drawable d = getResources().getDrawable(theBitmapIds[index]);
Someone help me pls!
You can use AssetManager to directly load assets with their paths.
You can get its instance by calling context.getAssets().
With it's open() method, you'll be able to get an InputStream for that asset.
Then you can use BitmapFactory.decodeStream() to convert it into Bitmap.
User contributions licensed under CC BY-SA 3.0