Skip to content Skip to sidebar Skip to footer

Getting The Byte Array Using Just The Image Name

I am stuck in a situation. I have some set of 10 images in drawable folder. During my code I have just the image name. for e.g 'elephant.png' and I want the byte array of the imag

Solution 1:

try as:

// get Drawable id intdrawableid=getResources().getIdentifier(GroupIconAdapter.mThumbIds[position], 
                       "drawable",this.getPackageName());

Drawabledrawable_img= getResources().getDrawable(drawableid);  // get DrawableBitmapdrawable_bitmap= ((BitmapDrawable)drawable_img).getBitmap();
ByteArrayOutputStreamoutstream=newByteArrayOutputStream();
drawable_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outstream);

// get byte array herebyte[] bytearray_of_drawable = outstream.toByteArray();

Solution 2:

Put this images into your assests directory and you can access like this way

Stringurl="file:///android_asset/elephant.png";

Solution 3:

I think this way should move right

First of all I should put my images in the assets folder and then making the drawbles for the same

String filePath="file:///android_asset/images/test.png";
Drawabled= Drawable.createFromPath(filePath);

and then doing the respective streamings to make a byte array -

Bitmapdrawable_bitmap= ((BitmapDrawable)d).getBitmap();
ByteArrayOutputStreamoutstream=newByteArrayOutputStream();
drawable_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outstream);

// get byte array herebyte[] bytearray_of_drawable = outstream.toByteArray();

This way we can get the image name's drawable and corresponding byte array.

Thanks

Post a Comment for "Getting The Byte Array Using Just The Image Name"