【问题标题】:Getting the Byte Array using just the image name仅使用图像名称获取字节数组
【发布时间】:2012-12-12 20:49:32
【问题描述】:
我陷入了困境。我在可绘制文件夹中有一组 10 张图像。
在我的代码中,我只有图像名称。例如“elephant.png”,我想要图像的字节数组(可绘制)对应于图像名称。
String imageName = getResources().getResourceEntryName(GroupIconAdapter.mThumbIds[position]);
从这里我得到图像名称,我想要字节数组。
有可能吗?
谢谢
【问题讨论】:
标签:
android
uiimageview
bytearray
【解决方案1】:
把这些图片放到你的assets目录下就可以这样访问了
String url = "file:///android_asset/elephant.png";
【解决方案2】:
尝试:
// get Drawable id
int drawableid =getResources().getIdentifier(GroupIconAdapter.mThumbIds[position],
"drawable",this.getPackageName());
Drawable drawable_img = getResources().getDrawable(drawableid); // get Drawable
Bitmap drawable_bitmap = ((BitmapDrawable)drawable_img).getBitmap();
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
drawable_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
// get byte array here
byte[] bytearray_of_drawable = outstream.toByteArray();
【解决方案3】:
我认为这条路应该向右移动
首先我应该把我的图片放在 assets 文件夹中,然后制作相同的 drawbles
String filePath="file:///android_asset/images/test.png";
Drawable d = Drawable.createFromPath(filePath);
然后进行相应的流式处理以制作一个字节数组 -
Bitmap drawable_bitmap = ((BitmapDrawable)d).getBitmap();
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
drawable_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
// get byte array here
byte[] bytearray_of_drawable = outstream.toByteArray();
这样我们就可以得到图片名称的drawable和对应的字节数组了。
谢谢