Example usage for android.os StatFs getAvailableBytes

List of usage examples for android.os StatFs getAvailableBytes

Introduction

In this page you can find the example usage for android.os StatFs getAvailableBytes.

Prototype

public long getAvailableBytes() 

Source Link

Document

The number of bytes that are free on the file system and available to applications.

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static long getSizeUpdatedApi(StatFs statFs) {
    return statFs.getAvailableBytes() / (1024 * 1024);
}

From source file:Main.java

/**
 * Get the number of tiles that can be stored on the file system.
 *
 * @param directory where the cache will reside
 * @param fileSize  average size of tile to be cached
 * @return number of tiles that can be stored without running out of space
 *///  w  w w.ja v  a  2  s . c  o m
@SuppressWarnings("deprecation")
@TargetApi(18)
public static long getAvailableCacheSlots(String directory, int fileSize) {
    StatFs statfs = new StatFs(directory);
    if (android.os.Build.VERSION.SDK_INT >= 18) {
        return statfs.getAvailableBytes() / fileSize;
    }
    // problem is overflow with devices with large storage, so order is important here
    // additionally avoid division by zero in devices with a large block size
    int blocksPerFile = Math.max(fileSize / statfs.getBlockSize(), 1);
    return statfs.getAvailableBlocks() / blocksPerFile;
}

From source file:Main.java

@SuppressWarnings("WeakerAccess")
public static long getAvailableBytes(File dir) {
    if (!dir.exists() && !dir.mkdirs()) {
        return 0;
    }/*from   w w  w .  jav a  2s . c  om*/
    StatFs dirStatFs = new StatFs(dir.getPath());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        return dirStatFs.getAvailableBytes();
    } else {
        //noinspection deprecation
        return (long) dirStatFs.getAvailableBlocks() * dirStatFs.getBlockSize();
    }
}

From source file:org.proninyaroslav.libretorrent.core.utils.FileIOUtils.java

public static long getFreeSpace(String path) {
    long availableBytes = -1L;

    try {/*from  w  ww. ja  v a 2  s.  co m*/
        File file = new File(path);
        availableBytes = file.getUsableSpace();
    } catch (Exception e) {

        // this provides invalid space on some devices
        try {
            StatFs stat = new StatFs(path);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                availableBytes = stat.getAvailableBytes();
            } else {
                availableBytes = stat.getAvailableBlocks() * stat.getBlockSize();
            }
        } catch (Exception ee) {
            /* Ignore */
        }
    }

    return availableBytes;
}

From source file:com.watabou.noosa.Game.java

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static long getAvailableInternalMemorySize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long ret;/*from w ww .  ja  v  a2  s  . co m*/
    if (android.os.Build.VERSION.SDK_INT < 18) {
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        ret = availableBlocks * blockSize;
    } else {
        ret = stat.getAvailableBytes();
    }
    Util.storeEventInAcra("FreeInternalMemorySize", Long.toString(ret));
    return ret;
}

From source file:com.fallahpoor.infocenter.fragments.StorageFragment.java

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private String getInternalFree() {

    long size;/*  w w  w  .j a  va 2  s .  com*/
    StatFs internalStatFs = new StatFs(Environment.getDataDirectory().getAbsolutePath());

    if (mIsApiAtLeast18) {
        size = internalStatFs.getAvailableBytes();
    } else {
        size = (long) internalStatFs.getAvailableBlocks() * (long) internalStatFs.getBlockSize();
    }

    return Utils.getFormattedSize(size);

}

From source file:com.fallahpoor.infocenter.fragments.StorageFragment.java

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private String getExternalFree() {

    long size;/*from   w  w  w .j  a va  2 s .  c om*/
    StatFs extStorageStatFs;
    String extStoragePath = getExternalStoragePath();

    if (extStoragePath == null || !(new File(extStoragePath).exists())) {
        return getString(R.string.sto_sub_item_ext_storage_not_available);
    }

    extStorageStatFs = new StatFs(new File(extStoragePath).getAbsolutePath());

    if (mIsApiAtLeast18) {
        size = extStorageStatFs.getAvailableBytes();
    } else {
        size = (long) extStorageStatFs.getAvailableBlocks() * (long) extStorageStatFs.getBlockSize();
    }

    return Utils.getFormattedSize(size);

}

From source file:cz.muni.fi.japanesedictionary.parser.ParserService.java

/**
 * Downloads dictionaries./* www.j av  a 2 s  . co m*/
 */

protected void onHandleIntent() {

    Log.i(LOG_TAG, "Creating parser service");

    Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mBuilder = new NotificationCompat.Builder(this).setAutoCancel(false).setOngoing(true)
            .setContentTitle(getString(R.string.dictionary_download_title))
            .setContentText(getString(R.string.dictionary_download_in_progress) + " (1/5)")
            .setSmallIcon(R.drawable.ic_notification).setProgress(100, 0, false).setContentInfo("0%")
            .setContentIntent(resultPendingIntent);

    startForeground(0, mNotification);
    mNotifyManager.notify(0, mBuilder.build());
    File storage;
    if (MainActivity.canWriteExternalStorage()) {
        // external storage available
        storage = getExternalCacheDir();
    } else {
        storage = getCacheDir();
    }
    if (storage == null) {
        throw new IllegalStateException("External storage isn't accessible");
    }
    // free sapce controll
    StatFs stat = new StatFs(storage.getPath());
    long bytesAvailable;
    if (Build.VERSION.SDK_INT < 18) {
        bytesAvailable = (long) stat.getBlockSize() * (long) stat.getAvailableBlocks();
    } else {
        bytesAvailable = stat.getAvailableBytes();
    }
    long megAvailable = bytesAvailable / 1048576;
    Log.d(LOG_TAG, "Megs free :" + megAvailable);
    if (megAvailable < 140) {
        mInternetReceiver = null;
        mNotEnoughSpace = true;
        stopSelf(mStartId);
        return;
    }

    this.registerReceiver(mInternetReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    boolean english = sharedPrefs.getBoolean("language_english", false);
    boolean french = sharedPrefs.getBoolean("language_french", false);
    boolean dutch = sharedPrefs.getBoolean("language_dutch", false);
    boolean german = sharedPrefs.getBoolean("language_german", false);
    boolean russian = sharedPrefs.getBoolean("language_russian", false);
    if (!english && !french && !dutch && !german && !russian) {
        Log.i(LOG_TAG, "Setting english as only translation language");
        SharedPreferences.Editor editor_lang = sharedPrefs.edit();
        editor_lang.putBoolean("language_english", true);
        editor_lang.commit();
    }

    String dictionaryPath;
    String kanjiDictPath;

    URL url;
    try {
        url = new URL(ParserService.DICTIONARY_PATH);
    } catch (MalformedURLException ex) {
        Log.e(LOG_TAG, "Error: creating url for downloading dictionary");
        return;
    }

    try {

        dictionaryPath = storage.getPath() + File.separator + "dictionary.zip";
        File outputFile = new File(dictionaryPath);
        if (outputFile.exists()) {
            outputFile.delete();
        }

        mDownloadJMDictFrom = url;
        mDownloadJMDictTo = outputFile;
        mDownloadingJMDict = true;
        mDownloadInProgress = true;

        // downloading kanjidict
        url = null;
        try {
            url = new URL(ParserService.KANJIDICT_PATH);
        } catch (MalformedURLException ex) {
            Log.e(LOG_TAG, "Error: creating url for downloading kanjidict2");
        }
        if (url != null) {
            kanjiDictPath = storage.getPath() + File.separator + "kanjidict.zip";
            File fileKanjidict = new File(kanjiDictPath);
            if (fileKanjidict.exists()) {
                fileKanjidict.delete();
            }
            mDownloadingKanjidic = false;
            mDownloadKanjidicFrom = url;
            mDownloadKanjidicTo = fileKanjidict;
        }

        mDownloadTatoebaIndicesFrom = new URL(ParserService.TATOEBA_INDICES_PATH);
        mDownloadTatoebaIndicesTo = new File(storage, "tatoeba-japanese.zip");
        if (mDownloadTatoebaIndicesTo.exists()) {
            mDownloadTatoebaIndicesTo.delete();
        }

        mDownloadTatoebaSentencesFrom = new URL(ParserService.TATOEBA_SENTENCES_PATH);
        mDownloadTatoebaSentencesTo = new File(storage, "tatoeba-translation.zip");
        if (mDownloadTatoebaSentencesTo.exists()) {
            mDownloadTatoebaSentencesTo.delete();
        }

        mDownloadKanjiVGFrom = new URL(ParserService.KANJIVG_PATH);
        mDownloadKanjiVGTo = new File(storage, "kanjivg.zip");
        if (mDownloadKanjiVGTo.exists()) {
            mDownloadKanjiVGTo.delete();
        }

        downloadDictionaries();

    } catch (MalformedURLException e) {
        Log.e(LOG_TAG, "MalformedURLException wrong format of URL: " + e.toString());
        stopSelf(mStartId);
    } catch (IOException e) {
        Log.e(LOG_TAG, "IOException downloading interrupted: " + e.toString());
        stopSelf(mStartId);
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(LOG_TAG, "Exception: " + e.toString());
        stopSelf(mStartId);
    }

}