Uploading base64 results in a outofmemory error in a Android app

xolympus

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!

Tristan Burnside

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Base64 encode file and OutOfMemory error

From Dev

Android: OutOfMemory error and the backstack

From Dev

Android Java Error OutOfMemory

From Dev

Android OutOfMemory error ImageView

From Dev

How to overcome OutOfMemory error from my android app.?

From Dev

How to overcome OutOfMemory error from my android app.?

From Dev

Error while uploading a new version of an Android app

From Dev

outofmemory error using bitmaps Android

From Dev

OutOfMemory android error with CircleImageView and setBackgroundResource

From Dev

Android encoding PNG to base64 string producing invalid results

From Dev

Error uploading ionic app

From Java

Uploading Android App Bundle to Google Play Console - key signing error

From Dev

Cordova/Ionic app uploading base64 image to S3 via server signed url

From Dev

OutOfMemory when uploading files

From Dev

Using Base64 encoded characters as filename in Android app

From Dev

Getting Base64 error when uploading an image asp.net MVC

From Dev

Java: Different results when decoding base64 string with java.util.Base64 vs android.util.Base64

From Dev

Android OutOfMemory Error while loading Bitmaps

From Dev

Android -OutofMemory error- bitmap factory

From Dev

Uploading a video to S3 AWS from Android app results in a 0Kb file

From Dev

Error while uploading tvOS app

From Dev

error uploading iOS app to the store

From Dev

Cloudinary Base64 Image uploading in angularjs

From Dev

Images(base64) not uploading correctly

From Dev

Uploading base64 to azure file share

From Dev

Cloudinary Base64 Image uploading in angularjs

From Dev

Getting error while uploading app on App Store

From Java

Android App Error when uploaded to Play Store , Before Uploading, tested everything was fine

From Dev

Uploading large file from Android app gives out of memory error despite buffering

Related Related

  1. 1

    Base64 encode file and OutOfMemory error

  2. 2

    Android: OutOfMemory error and the backstack

  3. 3

    Android Java Error OutOfMemory

  4. 4

    Android OutOfMemory error ImageView

  5. 5

    How to overcome OutOfMemory error from my android app.?

  6. 6

    How to overcome OutOfMemory error from my android app.?

  7. 7

    Error while uploading a new version of an Android app

  8. 8

    outofmemory error using bitmaps Android

  9. 9

    OutOfMemory android error with CircleImageView and setBackgroundResource

  10. 10

    Android encoding PNG to base64 string producing invalid results

  11. 11

    Error uploading ionic app

  12. 12

    Uploading Android App Bundle to Google Play Console - key signing error

  13. 13

    Cordova/Ionic app uploading base64 image to S3 via server signed url

  14. 14

    OutOfMemory when uploading files

  15. 15

    Using Base64 encoded characters as filename in Android app

  16. 16

    Getting Base64 error when uploading an image asp.net MVC

  17. 17

    Java: Different results when decoding base64 string with java.util.Base64 vs android.util.Base64

  18. 18

    Android OutOfMemory Error while loading Bitmaps

  19. 19

    Android -OutofMemory error- bitmap factory

  20. 20

    Uploading a video to S3 AWS from Android app results in a 0Kb file

  21. 21

    Error while uploading tvOS app

  22. 22

    error uploading iOS app to the store

  23. 23

    Cloudinary Base64 Image uploading in angularjs

  24. 24

    Images(base64) not uploading correctly

  25. 25

    Uploading base64 to azure file share

  26. 26

    Cloudinary Base64 Image uploading in angularjs

  27. 27

    Getting error while uploading app on App Store

  28. 28

    Android App Error when uploaded to Play Store , Before Uploading, tested everything was fine

  29. 29

    Uploading large file from Android app gives out of memory error despite buffering

HotTag

Archive