[英]Upload image from android to php server using base64 inside thread
I have a thread, whit a for-loop that is going through a object of file paths to images in the gallery. 我有一个线程,有一个for循环,它正在通过文件路径的对象到达图库中的图像。 The code inside the thread is:
线程内的代码是:
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());
}
}
The code executes. 代码执行。 And the first image is successfully uploaded, but when the thread tries to run again it crashes.
并且第一个图像已成功上传,但是当线程再次尝试运行时,它崩溃了。 LogCat output is:
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)
So one image works grate, more than one, not so much... Does anyone know what the solution to the problem might be? 因此,一幅图像能产生很好的效果,而不是一幅图像,而不是那么多...有人知道解决问题的方法是什么吗?
Thanks! 谢谢!
The problem is BitmapFactory.decodeFile(BitmapFactory.java:347)
as stated in logcat. 如logcat中所述,问题是
BitmapFactory.decodeFile(BitmapFactory.java:347)
。 There are a few solutions. 有一些解决方案。 First is to catch outofmemory , see this post for more information .
首先是要清除内存不足,有关更多信息,请参见此帖子 。 You can also decrease the size of your bitmaps.
您还可以减小位图的大小。 A great sample can be found in the docs
可以在文档中找到一个很好的示例
您可以使用ByteArrayInputStream(stringVariable.getBytes(charset))将其转换为InputStream。InputStream是= new ByteArrayInputStream(stringVariable.getBytes(StandardCharsets.UTF_8));
Bitmap FixBitmap;
String ImagePath = "image_path" ;
String ConvertImage ;
byte[] byteArray ;
ByteArrayOutputStream byteArrayOutputStream ;
and then you have to check 然后你必须检查
@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));
}
}
this is also, please 也请
@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);
** Do not add this code **请勿添加此代码
httpURLConnection.setRequestProperty("Content-Type", "image/jpeg");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.