将位图转换为字节数组android使应用程序崩溃

时间:2014-08-15 05:06:13

标签: android sockets bitmap bytearray

我正在尝试使用socket在Android设备之间发送图像。首先,选定的图像将转换为位图,然后转换为byte []发送,但位图的进程句柄总是使应用程序强制关闭

这是代码

File imgFile = new File("/storage/emulated/0/DCIM/Camera/beach.jpg");
FileInputStream fis = null;
try{
 fis = new FileInputStream(imgFile);
} catch (FileNotFoundException e){
 e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,100,stream);
imageByte = stream.toByteArray();
OutputStream os = socket.getOutputStream();
os.write(imageByte);
os.flush();

在真实设备上调试时,应用程序始终强制关闭bm.compress行(Bitmap.CompressFormat.JPEG,100,stream); 我尝试了其他方式:为位图创建缓冲区并分配ByteBuffer,但处理位图总是导致崩溃,如:bm.getByteCount()或甚至bm.getHeight()

有人请帮我解决这个问题吗?

否则,任何人都可以列出一些其他方法来在android上发送数据(图像,媒体......),我正在创建一个提供用户之间共享数据的交互应用程序。非常感谢。

1 个答案:

答案 0 :(得分:0)

谢谢大家,这只是一个错误,显示Toast导致应用程序崩溃,愚蠢的我!!!