我有一个致命的异常:AsyncTask#1尝试将图像上传到网络服务器从Android应用程序。我正在使用Bitmap和BitmapFactory对图像进行编码。
@Override
protected Void doInBackground(Void... params) {
Bitmap bitmapOrg = BitmapFactory.decodeFile(this.path);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
if (bitmapOrg == null)
{
return null;
}
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] data = bao.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost( "http://192.168.20.162/sse_server/up.php" );
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
//Set Data and Content-type header for the image
entity.addPart("file", new ByteArrayBody(data, "image/jpeg", "file"));
postRequest.setEntity(entity);
try {
HttpResponse response = httpClient.execute(postRequest);
//Read the response
String jsonString = EntityUtils.toString(response.getEntity());
Log.v("RequestInformation", "after uploading file " + jsonString);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
这是LogCat错误堆栈:
06-18 09:38:30.270: W/dalvikvm(1394): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
06-18 09:38:30.490: E/AndroidRuntime(1394): FATAL EXCEPTION: AsyncTask #1
06-18 09:38:30.490: E/AndroidRuntime(1394): java.lang.RuntimeException: An error occured while executing doInBackground()
06-18 09:38:30.490: E/AndroidRuntime(1394): at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-18 09:38:30.490: E/AndroidRuntime(1394): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-18 09:38:30.490: E/AndroidRuntime(1394): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-18 09:38:30.490: E/AndroidRuntime(1394): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-18 09:38:30.490: E/AndroidRuntime(1394): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-18 09:38:30.490: E/AndroidRuntime(1394): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-18 09:38:30.490: E/AndroidRuntime(1394): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-18 09:38:30.490: E/AndroidRuntime(1394): at java.lang.Thread.run(Thread.java:856)
06-18 09:38:30.490: E/AndroidRuntime(1394): Caused by: java.lang.OutOfMemoryError
06-18 09:38:30.490: E/AndroidRuntime(1394): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-18 09:38:30.490: E/AndroidRuntime(1394): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
06-18 09:38:30.490: E/AndroidRuntime(1394): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:302)
06-18 09:38:30.490: E/AndroidRuntime(1394): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:328)
06-18 09:38:30.490: E/AndroidRuntime(1394): at com.example.testapp.HttpTraitement.doInBackground(HttpTraitement.java:75)
06-18 09:38:30.490: E/AndroidRuntime(1394): at com.example.testapp.HttpTraitement.doInBackground(HttpTraitement.java:1)
06-18 09:38:30.490: E/AndroidRuntime(1394): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-18 09:38:30.490: E/AndroidRuntime(1394): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-18 09:38:30.490: E/AndroidRuntime(1394): ... 4 more
我不知道错误来自哪里。请帮忙
答案 0 :(得分:1)
位图是非常大的内存消费者。将两个加载到内存中可能是个大问题。解码新位图时应考虑使用BitmapFactory.Options。
这有助于https://stackoverflow.com/a/13363988/1503155