我已尝试 200 MB 的图像文件并将其转换为字节数组但由于OOM而崩溃,因此如何读取大文件并转换为byte []
引起:java.lang.OutOfMemoryError:无法分配210288697字节分配4108138个空闲字节和186MB直到OOM
byte[] fullyReadFileToBytes(File file) throws IOException {
int size = (int) file.length();
byte bytes[] = new byte[size];
byte tmpBuff[] = new byte[size];
FileInputStream fis = new FileInputStream(file);
try {
int read = fis.read(bytes, 0, size);
if (read < size) {
int remain = size - read;
while (remain > 0) {
read = fis.read(tmpBuff, 0, remain);
System.arraycopy(tmpBuff, 0, bytes, size - remain, read);
remain -= read;
}
}
} catch (IOException e) {
throw e;
} finally {
fis.close();
}
return bytes;
}
注意:我已经尝试过100MB并且它工作正常但是如果大小超过150MB则会造成崩溃。
答案 0 :(得分:2)
使用Http post multipart transfer,即
$(document).ready(function () {
// append plus symbol to every list item that has children
$('#mobile-nav .parent').append('<span class="open-menu fa fa-angle-down"></span>');
// fix non-scrolling overflow issue on mobile devices
$('#mobile-nav > ul').wrap('<div class="overflow"></div>');
$(window).on('load resize', function () {
var vph = $(window).height(); // 57px - height of #mobile-nav
$('.overflow').css('max-height', vph);
});
// global variables
var menu = $('.overflow > ul');
var bg = $('html, body');
// toggle background scrolling
function bgScrolling() {
// if menu has toggled class... *
if (menu.hasClass('open')) {
// * disable background scrolling
bg.css({
'overflow-y': 'hidden',
'height': 'auto'
});
// if menu does not have toggled class... *
} else {
// * enable background scrolling
bg.css({
'overflow-y': 'visible',
'height': '100%'
});
}
}
// menu button click events
$('.menu-button').on('click', function (e) {
e.preventDefault();
// activate toggles
menu.slideToggle(250);
menu.toggleClass('open');
bgScrolling();
});
// list item click events
$('.open-menu').on('click', function (e) {
e.preventDefault();
$(this).prev('ul').slideToggle(250);
$(this).toggleClass('rotate');
});
});
使用volley多次传输请求。它将跟踪故障 https://gist.github.com/anggadarkprince/a7c536da091f4b26bb4abf2f92926594
答案 1 :(得分:-3)
以下代码有助于压缩位图图像并转换为字符串字节。
public String BitMapToString(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
byte[] b = baos.toByteArray();
String temp = null;
try {
System.gc();
temp = Base64.encodeToString(b, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
b = baos.toByteArray();
temp = Base64.encodeToString(b, Base64.DEFAULT);
}
return temp;
}