3

I have my Android application which needs to send data to server after certain limit of memory is reached. I am checking internal memory left using -

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
long megAvailable = bytesAvailable / (1024 * 1024);
Log.e("","Available MB : "+ megAvailable);

Suppose Available MB initially is 800MB. I have an ArrayList of objects that keeps on adding elements every 10 seconds. Once my Available MB becomes 700MB, I want my data in ArrayList to be sent to server.

I wanted to know that when Available MB becomes 700MB-

a) how to retrieve data from ArrayList from 0th position until the current position(the position at which Available MB becomes 700MB) and send all at once.

b) After sending clear the arraylist and start storing new data until Available MB becomes 600MB

1 Answer 1

1

When you add data to the arraylist you can keep a count. And when you invoke the method to find out if you have touched 700 mb you can send that count as a parameter.

You can call the sublist method of ArrayList

int to = 4;
int from = 0;
al.subList(from, to);

You can use removeAll() to delete all the elements.

I exactly don't know how you can keep count of the the 'to' here. But in any case , when you add data to the array you can keep a counter and send that over to the method you want to invoke.

Sign up to request clarification or add additional context in comments.

2 Comments

How will i know toIndex position in sublist
When you add data to the arraylist you can keep a count. And when you invoke the method to find out if you have touched 700 mb tyou can send that count as a parameter. Does it make sense ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.