从蓝牙接收到的字节数组中创建位图,并将位图保存在sd卡中



我必须实现一个代码,其中我从外部h/w接收字节数组。设备,最后我必须从字节数组创建一个位图图像并将该位图存储在sdcard中。

File file=new File(Environment.getExternalStorageDirectory(), "file.jpeg");
        if (file.exists()) {
            file.delete();
        }
        try {
            fos = new FileOutputStream(file.getPath());
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        try {
            fos.write(buffer);
            //bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.close();
            //bitmap = BitmapFactory.decodeByteArray(buffer , 0, buffer.length);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        } 

文件在sdcard中创建,但该文件没有显示图像。

bitmap = BitmapFactory.decodeByteArray(buffer , 0, buffer.length);
            OutputStream stream = new FileOutputStream("/sdcard/test.jpg");
            bitmap.compress(CompressFormat.JPEG, 100, stream);

Try this Let image包含位图图像

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();

imageInByte现在包含位图图像的字节数据。

用于转换反向

Bitmap bp = BitmapFactory.decodeByteArray(imgArray, 0,imgArray.length);希望这对你有帮助

更新:

存储在Sd卡上

Bitmap bmp = createBitmap();
OutputStream stream = new FileOutputStream("/sdcard/test.jpg");
bmp.compress(CompressFormat.JPEG, 100, stream);

试试这样:

FileInputStream in;    
BufferedInputStream buf;    
try {      
    in = new FileInputStream("/sdcard/pictures/1.jpg");    
    buf = new BufferedInputStream(in,1070);    
    byte[] bMapArray= new byte[buf.available()];   
    buf.read(bMapArray);     
    Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length); 
}catch (Exception e) {        
    Log.e("Error reading file", e.toString());      
} 

最新更新