Added "free memory" check before downloading [Android N / API 24+] (#10505)
Added "free space" check before downloading eliminating bugs related to out-of-memory on Android N / API 24+
This commit is contained in:
parent
5bdb6f18d6
commit
2e318b8b03
3 changed files with 82 additions and 4 deletions
|
|
@ -2,6 +2,8 @@ package us.shandian.giga.util;
|
|||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.os.Environment;
|
||||
import android.os.StatFs;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.ColorInt;
|
||||
|
|
@ -26,10 +28,8 @@ import java.io.ObjectOutputStream;
|
|||
import java.io.Serializable;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
|
||||
import okio.ByteString;
|
||||
import us.shandian.giga.get.DownloadMission;
|
||||
|
||||
public class Utility {
|
||||
|
||||
|
|
@ -40,6 +40,20 @@ public class Utility {
|
|||
UNKNOWN
|
||||
}
|
||||
|
||||
/**
|
||||
* Get amount of free system's memory.
|
||||
* @return free memory (bytes)
|
||||
*/
|
||||
public static long getSystemFreeMemory() {
|
||||
try {
|
||||
final StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath());
|
||||
return statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong();
|
||||
} catch (final Exception e) {
|
||||
// do nothing
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static String formatBytes(long bytes) {
|
||||
Locale locale = Locale.getDefault();
|
||||
if (bytes < 1024) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue