I'm back with a problem.
I'm trying to create a Android application that allows the user to search a image from their gallery and upload it. However on high resolution images the application will crash with a out of memory error.
The image is 1,7MB and a resolution of 1944x1944px.
After the users opens a picture from their gallery it will be loaded in a imageview, I already resized the picture to 150x150 pixels to prevent crashes there.
Code:
Button buttonUploadImage = (Button) findViewById(R.id.uploadImage);
buttonUploadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
dialog = ProgressDialog.show(MainActivity.this, "",
"Uploading...", true);
new Thread(){
@Override
public void run(){
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); bitmap.recycle();
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeToString(byte_arr, columnIndex2);
bitmap = null;
System.gc();
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/android.php");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("image", image_str));
httppost.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = httpclient.execute(httppost);
inputStream = response.getEntity().getContent();
//Json stuff here to handle the response and see if it succeeded or not.
Error message: (I hope this is allowed) Error message I already tried bitmap = null and System.gc() but both didn't help.
Ofcourse I can upload it in chuncks, but shouldn't Android be able to upload a 1.7MB image without any problems?
Thanks already!
if you are finished with the bitmap, bitmap.recycle()
is probably what you are looking for.
check this for more info on managing bitmap memory.
https://developer.android.com/training/displaying-bitmaps/manage-memory.html
Edit: I just noticed you are calling that (two logical statements on one line is quite hard to read).
Second Edit: It seems the issue is that you are making several representations of the image other than the bitmap (a byte array, a string and a URLEncodedFormEntity), the System.gc() call is probably cleaning up the byte array. A quick google provided an example that is using a buffer to send a file http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments