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);
}
}