我有一个线程,带有一个 for 循环,它通过文件路径对象到画廊中的图像。线程内部的代码是:
for(int i=0;i<tellerList;i++){
Log.e("Picture", x.get(i));
TAG_PICTURE = x.get(i);
Bitmap bitmap = BitmapFactory.decodeFile(TAG_PICTURE);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
double hoyde1 = bitmap.getHeight();
double bredde1 = bitmap.getWidth();
int hoyde = (int) (hoyde1 / 1.5);
int bredde = (int) (bredde1 / 1.5);
Bitmap bitmapfinal = Bitmap.createScaledBitmap(bitmap, bredde,hoyde, false);
bitmapfinal.compress(Bitmap.CompressFormat.JPEG, 80, stream);
byte[] byte_arr = stream.toByteArray();
try {
stream.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String image_str = Base64.encodeToString(byte_arr,
Base64.DEFAULT);
// HTTP POST
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("tag", login_tag));
param.add(new BasicNameValuePair("unique_id", in
.getStringExtra(TAG_UID)));
param.add(new BasicNameValuePair("picture_data", image_str));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(param));
Log.e("Ferdig med å laste opp", "Ferdig");
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
}
代码执行。第一张图片已成功上传,但当线程再次尝试运行时,它崩溃了。LogCat 输出为:
01-06 18:14:47.113: E/dalvikvm(5010): Out of memory: Heap Size=58531KB, Allocated=50243KB, Limit=65536KB
01-06 18:14:47.113: E/dalvikvm(5010): Extra info: Footprint=56483KB, Allowed Footprint=58531KB, Trimmed=3712KB
01-06 18:14:47.113: D/skia(5010): --- decoder->decode returned false
01-06 18:14:47.113: W/dalvikvm(5010): threadid=47: thread exiting with uncaught exception (group=0x40d782d0)
01-06 18:14:47.113: E/AndroidRuntime(5010): FATAL EXCEPTION: Thread-543
01-06 18:14:47.113: E/AndroidRuntime(5010): java.lang.OutOfMemoryError: (Heap Size=58531KB, Allocated=50244KB)
01-06 18:14:47.113: E/AndroidRuntime(5010): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
01-06 18:14:47.113: E/AndroidRuntime(5010): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:658)
01-06 18:14:47.113: E/AndroidRuntime(5010): at
android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:347)
01-06 18:14:47.113: E/AndroidRuntime(5010): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:430)
01-06 18:14:47.113: E/AndroidRuntime(5010): at com.example.socializerfragments.PhotoUploadSelecter.run(PhotoUploadSelecter.java:96)
01-06 18:14:47.113: E/AndroidRuntime(5010): at java.lang.Thread.run(Thread.java:864)
所以一张图片很有效,不止一张,不是很多......有谁知道问题的解决方案可能是什么?
谢谢!