我正在寻找一些关于使我的应用程序对 RAM 更友好的建议。在更大的 android 平板设备上,该应用程序运行良好,我正在运行 Android 2.3.5 的 HTC Desire S 上进行测试,按照今天的标准,我知道这是一部古老的手机,但每当我尝试运行这种方法时,手机都会强制关闭。我想知道人们是否可以提供任何建议来修改我的代码以使其在 RAM 方面更有效率。
请看下面我的 android 方法和我的 logcat 打印输出:
安卓活动方法
public void uploadDataToCrowdSnapCymru() throws UnsupportedEncodingException {
ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("name", nameString));
nameValuePair.add(new BasicNameValuePair("desc", descString));
nameValuePair.add(new BasicNameValuePair("lat", latString));
nameValuePair.add(new BasicNameValuePair("lng", lngString));
nameValuePair.add(new BasicNameValuePair("project", projectString));
ByteArrayOutputStream bao = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
nameValuePair.add(new BasicNameValuePair("snap", ba1));
try {
//EXECUTE POST
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://users.aber.ac.uk/dwd/mfb/php/receiveUserData.php");
httppost.setHeader("Content-Type",
"application/x-www-form-urlencoded;");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
String responseText = EntityUtils.toString(response.getEntity());
System.out.println(responseText);
if(responseText != null){
Toast.makeText(this, "Record uploaded! View it at CrowdSnapCymru.com", Toast.LENGTH_LONG).show();
}
}
catch(Exception e) {
Log.e("log_tag", "Error in http connection"+e.toString());
}
}
LogCat 打印输出
05-15 23:52:55.938: E/dalvikvm(8950): Out of memory: Heap Size=9927KB, Allocated=5369KB, Bitmap Size=8756KB, Limit=20480KB
05-15 23:52:55.938: E/dalvikvm(8950): Extra info: Footprint=9927KB, Allowed Footprint=9927KB, Trimmed=504KB
05-15 23:52:55.938: D/AndroidRuntime(8950): Shutting down VM
05-15 23:52:55.938: W/dalvikvm(8950): threadid=1: thread exiting with uncaught exception (group=0x400205a0)
05-15 23:52:55.938: E/AndroidRuntime(8950): FATAL EXCEPTION: main
05-15 23:52:55.938: E/AndroidRuntime(8950): java.lang.OutOfMemoryError: (Heap Size=9927KB, Allocated=5369KB, Bitmap Size=8756KB)
05-15 23:52:55.938: E/AndroidRuntime(8950): at java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:122)
05-15 23:52:55.938: E/AndroidRuntime(8950): at rcahmw.prototype.crowdsnapcymru.UploadRecord.uploadDataToCrowdSnapCymru(UploadRecord.java:119)
05-15 23:52:55.938: E/AndroidRuntime(8950): at rcahmw.prototype.crowdsnapcymru.UploadRecord$1.onClick(UploadRecord.java:85)
05-15 23:52:55.938: E/AndroidRuntime(8950): at android.view.View.performClick(View.java:2532)
05-15 23:52:55.938: E/AndroidRuntime(8950): at android.view.View$PerformClick.run(View.java:9293)
05-15 23:52:55.938: E/AndroidRuntime(8950): at android.os.Handler.handleCallback(Handler.java:587)
05-15 23:52:55.938: E/AndroidRuntime(8950): at android.os.Handler.dispatchMessage(Handler.java:92)
05-15 23:52:55.938: E/AndroidRuntime(8950): at android.os.Looper.loop(Looper.java:150)
05-15 23:52:55.938: E/AndroidRuntime(8950): at android.app.ActivityThread.main(ActivityThread.java:4277)
05-15 23:52:55.938: E/AndroidRuntime(8950): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 23:52:55.938: E/AndroidRuntime(8950): at java.lang.reflect.Method.invoke(Method.java:507)
05-15 23:52:55.938: E/AndroidRuntime(8950): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-15 23:52:55.938: E/AndroidRuntime(8950): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
任何人可以提供的任何建议都会受到好评。最后一个问题是,HTTP POST
方法是否适用于移动网络和 Wi-Fi?谢谢。我假设它们是作为互联网浏览器应用程序在网络和 wifi 上运行的,并且它们的原理相同。