如何图像转换成Android中字节数组 [英] How to convert Image into Byte Array in Android
本文介绍了如何图像转换成Android中字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在面临一个真问题。我需要图像转换成字节数组格式,这样我就可以上传字节数组到Web服务器。我已经尝试了很多但是它不工作。我也越来越为字节数组负值。我不知道我做错了采取阵列字节值。
下面是我的code。请帮我什么,我做错了什么?
BMP位图= BitmapFactory.de codeResource(getResources(),R.drawable.home_menu_icon);
ByteArrayOutputStream BOS =新ByteArrayOutputStream();
bmp.com preSS(Bitmap.Com pressFormat.JPEG,100,BOS);
字节[]数据= bos.toByteArray();
解决方案
下面我展示code两种,如果你的形象是从画廊和CAMERA
如果(要求code == IMAGE_PICKER_REQUEST&放大器;&安培;结果code == RESULT_OK){
文件名= getRealPathFromURI(data.getData()); 尝试{
如果(位图!= NULL){
bitmap.recycle();
}
InputStream的流= getContentResolver()。openInputStream(
data.getData()); 位= BitmapFactory.de codeStream(流);
stream.close();
image.setImageBitmap(位图);
//picNameText.setText(\"Selected:EN
// + getStringNameFromRealPath(文件名)); ByteArrayOutputStream流1 =新ByteArrayOutputStream();
bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,流1);
imageInByte = stream1.toByteArray(); }赶上(FileNotFoundException异常五){
e.printStackTrace();
}
赶上(IOException异常五){
e.printStackTrace();
}
} 如果(要求code == IMAGE_TAKER_REQUEST&放大器;&安培;结果code == RESULT_OK){
。位图的照片=(位图)data.getExtras()获得(数据);
image.setImageBitmap(照片); ByteArrayOutputStream流2 =新ByteArrayOutputStream();
photo.com preSS(Bitmap.Com pressFormat.PNG,100,流2);
imageInByte = stream2.toByteArray(); }
享受它为我工作.....
I am facing one real issue. I need to convert image into byte array format, so that I can upload the byte array into web server. I have tried a lot however its not working. I am also getting negative values for byte array. I am not sure what i am doing wrong to take byte values in array.
Below is my code. Please help me what i am doing wrong?
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.home_menu_icon);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
解决方案
Here i show code for both if your image is from GALLERY and CAMERA
if (requestCode == IMAGE_PICKER_REQUEST && resultCode == RESULT_OK) {
fileName = getRealPathFromURI(data.getData());
try {
if (bitmap != null) {
bitmap.recycle();
}
InputStream stream = getContentResolver().openInputStream(
data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
image.setImageBitmap(bitmap);
//picNameText.setText("Selected: en"
// + getStringNameFromRealPath(fileName));
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1);
imageInByte = stream1.toByteArray();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
if (requestCode == IMAGE_TAKER_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
image.setImageBitmap(photo);
ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream2);
imageInByte = stream2.toByteArray();
}
enjoy it work for me.....
这篇关于如何图像转换成Android中字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文