我有一个线程,一个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)
因此,一个图像不仅仅是一个,而不是一个......有没有人知道该问题的解决方案可能是什么?
谢谢!
答案 0 :(得分:0)
问题是BitmapFactory.decodeFile(BitmapFactory.java:347)
,如logcat中所述。有一些解决方案。首先是要记住outofmemory,请参阅此post以获取更多信息。您还可以减小位图的大小。可以在docs
答案 1 :(得分:0)
您可以使用ByteArrayInputStream将其转换为InputStream(stringVariable.getBytes(charset)) InputStream = new ByteArrayInputStream(stringVariable.getBytes(StandardCharsets.UTF_8));
答案 2 :(得分:0)
Bitmap FixBitmap;
String ImagePath = "image_path" ;
String ConvertImage ;
byte[] byteArray ;
ByteArrayOutputStream byteArrayOutputStream ;
然后你必须检查
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FROM_FILE && resultCode == RESULT_OK && null != data)
{
Uri uri = data.getData();
String[] prjection ={MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri,prjection,null,null,null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(prjection[0]);
ImagePath = cursor.getString(columnIndex);
cursor.close();
FixBitmap = BitmapFactory.decodeFile(ImagePath);
ShowSelectedImage = (ImageView)findViewById(R.id.imageView);
ShowSelectedImage.setImageBitmap(BitmapFactory.decodeFile(ImagePath));
}
}
这也是,请
@Override
protected String doInBackground(Void... params) {
FixBitmap = BitmapFactory.decodeFile(ImagePath);
//FixBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gv); // this code will upload the pic to server that in drawable place
byteArrayOutputStream = new ByteArrayOutputStream();
FixBitmap.compress(Bitmap.CompressFormat.JPEG, 60, byteArrayOutputStream); //compress to 50% of original image quality
byteArray = byteArrayOutputStream.toByteArray();
ConvertImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
**不要添加此代码
httpURLConnection.setRequestProperty("Content-Type", "image/jpeg");