gpt4 book ai didi

android - java.lang.outofmemoryerror 位图大小超出位图上的 vm 预算

转载 作者:太空狗 更新时间:2023-10-29 15:32:59 25 4
gpt4 key购买 nike

在我的应用程序中,我正在显示来自厨房的图像,并且在选择图像时我想将该图像上传到网络服务器。为了将图像上传到服务器,我使用了以下代码,但我在bitmapImage = BitmapFactory.decodeFile(path,opt);

private void uploadImage(String selectedImagePath) {
String str = null;
byte[] data = null;
String Responce= null;
Bitmap bitmap2 = null;
try {
File file=new File(selectedImagePath);
//FileInputStream fileInputStream = new FileInputStream(new File(imagePath2) );
FileInputStream fileInputStream=new FileInputStream(selectedImagePath);
Log.i("Image path 2",""+selectedImagePath+"\n"+fileInputStream);
name=file.getName();
name=name.replace(".jpg","");
name=name.concat(sDate).concat(".jpg");
Log.e("debug",""+name);

//else
//{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];

//bitmapImage = BitmapFactory.decodeFile(path,opt);

bitmap2=BitmapFactory.decodeFileDescriptor(fd, outPadding, opts)
Log.i("Bitmap",""+bitmap.toString());
BitmapFactory.decodeStream(fileInputStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap2.compress(Bitmap.CompressFormat.PNG, 100, baos);
data = baos.toByteArray();
str=Base64.encodeBytes(data);
//}

//String image=str.concat(sDate);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",str));
nameValuePairs.add(new BasicNameValuePair("imagename", name));
Log.e("debug",""+nameValuePairs.toString());

HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost("http://ufindfish.b4live.com/uploadTipImage.php");

post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse httpResponse=client.execute(post);
HttpEntity entity=httpResponse.getEntity();
InputStream inputStream=entity.getContent();

StringBuffer builder=new StringBuffer();
int ch;
while( ( ch = inputStream.read() ) != -1 )
{
builder.append((char)ch);
}
String s=builder.toString();
Log.i("Response",""+s);



} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bitmap2!=null)
{
bitmap2.recycle();
}

最佳答案

错误是由于图像的大小,我使用此代码在从图库中选择时减小图像的大小。

public Bitmap setImageToImageView(String filePath) 
{
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);

// The new size we want to scale to
final int REQUIRED_SIZE = 1024;

// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true)
{
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}

// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);
return bitmap;

}

希望对您有所帮助。

关于android - java.lang.outofmemoryerror 位图大小超出位图上的 vm 预算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7400754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com