1

我必须实现一个代码,在该代码中我从外部硬件接收字节数组。设备,最后我必须从字节数组创建位图图像并将该位图存储在 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);
4

2 回答 2

0

试试这个让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);
于 2013-01-04T09:26:47.860 回答
0

尝试这样的事情:

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());      
} 
于 2013-01-04T09:15:52.467 回答