目前,当我想将位图保存到磁盘时,我使用:
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
是否可以将数据写入字节数组?我需要处理进入磁盘的数据,而不仅仅是位图的图像数据。jpg 包含其他内容,例如元数据。我真的不在乎它是jpg。我对数据是什么不感兴趣,而只是访问通常会写入磁盘的整个数据。
您可以使用:
public byte[] convertBitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(bitmap.getWidth() * bitmap.getHeight());
bitmap.compress(CompressFormat.PNG, 100, buffer);
return buffer.toByteArray();
}
如果你想保留原始像素数据(不压缩),你可以试试这个:
public byte[] bitmapToByteArray(Bitmap bitmap) {
ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(byteBuffer);
return byteBuffer.array();
}
您可以使用此代码:
ByteArrayOutputStream out = new ByteArrayOutputStream();
bMap.compress(Bitmap.CompressFormat.PNG, 100, out);
byte[] imageArray = out.toByteArray();
U can try this.
if (Utility.isWifiPresent()
|| Utility.isMobileConnectionPresent()) {
URL url = new URL(fileUrl);
InputStream iStream = url.openConnection().getInputStream();// .read(data)
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] tmpArray = new byte[1024];
int nRead;
while ((nRead = iStream.read(tmpArray, 0, tmpArray.length)) != -1) {
buffer.write(tmpArray, 0, nRead);
}
buffer.flush();
data = buffer.toByteArray();
FileOutputStream fOut = null;
//path to store
fOut = Utility.getFileOutputStreamForCloud(
sdcardFolderPath, fileUrl);
}
fOut.write(data);
fOut.flush();
fOut.close();