我的问题可能是老问题,但由于过去 2 周的这个问题,我现在很痛苦,现在它太多了,在我的应用程序中,我需要将大图像位图发送到服务器,我通过以下编码来做到这一点:
BitmapFactory.Options bfOptions=new BitmapFactory.Options();
bfOptions.inDither=false; //Disable Dithering mode
bfOptions.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inTempStorage=new byte[32 * 1024];
File file=new File(TabGroupActivity.path);
FileInputStream input=null;
try {
input = new FileInputStream(TabGroupActivity.path);
} catch (FileNotFoundException e) {
//TODO do something intelligent
e.printStackTrace();
}
if(input!=null)
{
bitmap = BitmapFactory.decodeStream(input, null, bfOptions);
}
Matrix mat = new Matrix();//removing rotations
if(TabGroupActivity.rotation==90 || TabGroupActivity.rotation==270)
{
mat.setRotate(90);
}
else if(TabGroupActivity.rotation==0 || TabGroupActivity.rotation==180)
{
mat.setRotate(0);
}
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mat, true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int height= bitmap.getHeight();
int width=bitmap.getWidth();
{
//formula for calculating aspect ratio
float k= (float) height/width;
newHeight =Math.round(620*k);
}
byte[] buffer=new byte[10];
while(input.read(buffer)!=-1)
{
bos.write(buffer);
}
bitmap = Bitmap.createScaledBitmap(bitmap , 620, newHeight, true);
bitmap.compress(CompressFormat.JPEG, 90, bos);
byte[] imageData = bos.toByteArray();
ContentBody cb = new ByteArrayBody(imageData, "image/jpg", "image1.jpeg");
input.close();
但是在发送 2,3 张图像后,我出现内存不足错误,BitmapFactory.decodeStream()
请帮我解决这个问题,主要是我无法重新调整大小或裁剪图像,我只需要将高质量的图像发送到服务器。