我试图在隐写术上做一个项目,其中在将信息隐藏在图像中之后,我想将图像发送到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)
这里出了什么问题?
答案 0 :(得分:1)
Android为每个应用的堆大小设置了硬限制。最正确 堆大小限制因设备而异,具体取决于RAM的大小 设备总体可用。如果您的应用已到达堆 容量和尝试分配更多的内存,它会收到一个 的OutOfMemoryError。
您应增加堆大小并缩小图像大小。希望这会有所帮助。