gpt4 book ai didi

android - Asynctask 中的 java.lang.OutOfMemoryError 错误

转载 作者:行者123 更新时间:2023-11-30 01:19:46 26 4
gpt4 key购买 nike

我正在尝试做一个关于隐写术的项目,在其中隐藏图像中的消息后,我想将图像发送到 pc 中的 wamp 服务器。以下是我用来将图像发送到 wamp 服务器的 AsyncTask,我在其中遇到了致命异常:

private class UploadImage extends AsyncTask<Void, Void, Void> {

Bitmap image;
String name;
ArrayList<NameValuePair> dataToSend = new ArrayList();

public UploadImage(String name){

String path = Environment.getExternalStorageDirectory().toString();
path = path+"/stegimg1.png";
Bitmap SendImg1 = BitmapFactory.decodeFile(path);

this.image = SendImg1;
this.name = name;

//Toast.makeText(getApplicationContext(), "Constructor - UpImg",Toast.LENGTH_SHORT).show();
}

@Override
protected Void doInBackground(Void... arg0) {


HttpParams httpRequestParams = getHttpRequestParams();
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVERADDRESS + "/SavePicture.php");

try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}catch(Exception e)
{
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub

Toast.makeText(getApplicationContext(), "Image sent to the server",Toast.LENGTH_SHORT).show();
super.onPostExecute(result);
//Toast.makeText(getApplicationContext(), "Image Uploaded" , Toast.LENGTH_SHORT).show();
}

@Override
protected void onPreExecute() {

Toast.makeText(getApplicationContext(), "Inside OnPreEXEC" , Toast.LENGTH_SHORT).show();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, baos);
String encodedImage = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);



dataToSend.add(new BasicNameValuePair("image", encodedImage));
dataToSend.add(new BasicNameValuePair("name", name));
super.onPreExecute();

}

我得到的 Logcat 输出是

05-17 16:45:39.077: E/AndroidRuntime(1860): FATAL EXCEPTION: AsyncTask #1
05-17 16:45:39.077: E/AndroidRuntime(1860): java.lang.RuntimeException: An error occured while executing doInBackground()
05-17 16:45:39.077: E/AndroidRuntime(1860): at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-17 16:45:39.077: E/AndroidRuntime(1860): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-17 16:45:39.077: E/AndroidRuntime(1860): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-17 16:45:39.077: E/AndroidRuntime(1860): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-17 16:45:39.077: E/AndroidRuntime(1860): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-17 16:45:39.077: E/AndroidRuntime(1860): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-17 16:45:39.077: E/AndroidRuntime(1860): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-17 16:45:39.077: E/AndroidRuntime(1860): at java.lang.Thread.run(Thread.java:856)
05-17 16:45:39.077: E/AndroidRuntime(1860): Caused by: java.lang.OutOfMemoryError
05-17 16:45:39.077: E/AndroidRuntime(1860): at java.lang.String.<init>(String.java:365)
05-17 16:45:39.077: E/AndroidRuntime(1860): at java.lang.String.<init>(String.java:228)
05-17 16:45:39.077: E/AndroidRuntime(1860): at android.util.Base64.encodeToString(Base64.java:456)
05-17 16:45:39.077: E/AndroidRuntime(1860): at com.msrohit.imageselect.MainActivity$UploadImage.doInBackground(MainActivity.java:209)

这里出了什么问题??

最佳答案

Android sets a hard limit on the heap size for each app. The exact heap size limit varies between devices based on how much RAM the device has available overall. If your app has reached the heap capacity and tries to allocate more memory, it will receive an OutOfMemoryError.

您应该增加堆大小并减少图像大小。希望这会有所帮助。

关于android - Asynctask 中的 java.lang.OutOfMemoryError 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37282258/

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