-1

I need to upload all my images from my phone to server but problem is I run up of memory with Async.

I saw alot solutions where they ask the user to make the image smaller but i need the original image to be sent not compress or modified.

I tried to Thread.sleep(5000);

I also tried the following but the problem is it's cleaning my Bitmap before it's uploaded since it's async

bm.recycle(); bm = null;

Is there anyway I can upload the image ONE BY ONE

     @Override
        protected void onCreate(Bundle savedInstanceState) {


    picturePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()+"/Camera/";
            Log.d("Files", "Path: " + picturePath);
            File f = new File(picturePath);
            File file[] = f.listFiles();
            Log.d("Files", "Size: " + file.length);
            for (int i=0; i < file.length; i++)
            {
                Log.d("Files", "FileName:" + file[i].getName());

                // Image location URL
                Log.e("Files", "----------------" + picturePath+file[i].getName());



         // Image
                    Bitmap bm = BitmapFactory.decodeFile(picturePath + file[i].getName());
                    ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 75, bao);
                    byte[] ba = bao.toByteArray();
                    ba1 = Base64.encodeToString(ba, Base64.DEFAULT);

                    Log.d("Files", "Uploading");

                    new uploadToServer().execute();

                }

                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
            }



     public class uploadToServer extends AsyncTask<Void, Void, String> {


            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected String doInBackground(Void... params) {

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("base64", ba1));
                nameValuePairs.add(new BasicNameValuePair("ImageName", System.currentTimeMillis() + ".jpg"));
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(URL);
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    String st = EntityUtils.toString(response.getEntity());
                    Log.v("Files", "In the try Loop" + st);

                } catch (Exception e) {
                    Log.v("Files", "Error in http connection " + e.toString());
                }
                return "Success";

            }

            protected void onPostExecute(String result) {
                super.onPostExecute(result);

            }
        }
CodeGuru
  • 3,645
  • 14
  • 55
  • 99
  • which exactly line causes OutOfMemory ? – AterLux Jun 16 '15 at 15:11
  • You're creating a bitmap called "bm", but you're not doing anything with it... you're basically uploading an empty byte array... – Gil Moshayof Jun 16 '15 at 15:12
  • 5
    You shouldn't decode the Bitmap at all - you want to upload a file, not display an image. Use a BufferedInputStream wrapping a FileInputStream with the target file - the read(byte[] buffer, int byteOffset, int byteCount) method allows you to write to a websocket in chunks. – npace Jun 16 '15 at 15:16
  • @GilMoshayof My bad, edited – CodeGuru Jun 16 '15 at 15:19
  • ba1 is of what type and is it global variable – Pankaj Jun 16 '15 at 15:19
  • @npace Thanks for the advice any code sample – CodeGuru Jun 16 '15 at 15:20
  • @npace http://stackoverflow.com/questions/27033764/how-to-upload-image-and-video-file-using-httppost-in-android like this? – CodeGuru Jun 16 '15 at 15:22
  • 1
    look at this question http://stackoverflow.com/questions/9630430/upload-large-file-in-android-without-outofmemory-error – tyczj Jun 16 '15 at 15:22
  • Sorry, I was thinking of a direct socket connection. If your API requires a POST request with the Base64 encoded image, look at @tyczj's comment. – npace Jun 16 '15 at 15:29
  • @tyczj Hi, I tried your suggestion it worked. Mind making it your answer? – CodeGuru Jun 16 '15 at 17:38

1 Answers1

1

You need to send your data in chunks using HttpUrlConnection's .setChunkedStreamingMode(1024);

see this answer for an example

Community
  • 1
  • 1
tyczj
  • 71,600
  • 54
  • 194
  • 296