我必须实现一个代码,我从外部h / w接收字节数组。我必须在字节数组中创建一个位图图像并将该位图存储在SD卡中。
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();
}
文件在SD卡中创建,但该文件未显示图像。
bitmap = BitmapFactory.decodeByteArray(buffer , 0, buffer.length);
OutputStream stream = new FileOutputStream("/sdcard/test.jpg");
bitmap.compress(CompressFormat.JPEG, 100, stream);
答案 0 :(得分: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());
}
答案 1 :(得分: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);