Example usage for android.content Context getCacheDir

List of usage examples for android.content Context getCacheDir

Introduction

In this page you can find the example usage for android.content Context getCacheDir.

Prototype

public abstract File getCacheDir();

Source Link

Document

Returns the absolute path to the application specific cache directory on the filesystem.

Usage

From source file:com.popdeem.sdk.uikit.activity.PDUIClaimActivity.java

public static Uri saveBitmapToInternalCache(Context context, Bitmap bitmap) {
    Uri targetUri = null;//www  . ja v  a 2s.c om
    FileOutputStream fos = null;
    try {
        File imgFile = File.createTempFile(String.valueOf(System.currentTimeMillis()), ".png",
                context.getCacheDir());
        //imgFile.createNewFile();
        fos = new FileOutputStream(imgFile);
        boolean compressed = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        if (compressed)
            targetUri = Uri.fromFile(imgFile);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null)
                fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return targetUri;
}

From source file:com.colorchen.qbase.utils.FileUtil.java

/**
 * ???/?,?/*from w w w .  ja  v a  2 s. c o  m*/
 * ?
 *
 * @return
 */
public static File getCecheFolder(Context context) {
    File folder = new File(context.getCacheDir(), "IMAGECACHE");
    if (!folder.exists()) {
        folder.mkdir();
    }
    return folder;
}

From source file:com.colorchen.qbase.utils.FileUtil.java

/**
 * /*from ww  w.  j a  va 2s  . c  om*/
 * <p>/data/data/com.xxx.xxx/cache</p>
 *
 * @return {@code true}: ?<br>{@code false}: 
 */
public static boolean cleanInternalCache(Context context) {
    return FileUtil.deleteFilesInDir(context.getCacheDir());
}

From source file:org.zoumbox.mh_dla_notifier.Receiver.java

protected void checkForWidgetsUpdate(Context context, Troll troll) {

    try {/*from ww  w .  j  a va2  s  . co m*/
        AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);

        ComponentName componentName = new ComponentName(context, HomeScreenWidget.class);
        int[] appWidgetIds = widgetManager.getAppWidgetIds(componentName);

        if (appWidgetIds != null && appWidgetIds.length > 0) {

            // FIXME AThimel 14/02/14 Remove ASAP
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            String dlaText = Trolls.getWidgetDlaTextFunction(context).apply(troll);
            Bitmap blason = MhDlaNotifierUtils.loadBlasonForWidget(troll.getBlason(), context.getCacheDir());

            for (int appWidgetId : appWidgetIds) {

                RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.home_screen_widget);
                views.setTextViewText(R.id.widgetDla, dlaText);

                if (blason == null) {
                    views.setImageViewResource(R.id.widgetImage, R.drawable.trarnoll_square_transparent_128);
                } else {
                    views.setImageViewBitmap(R.id.widgetImage, blason);
                }

                // Tell the AppWidgetManager to perform an update on the current app widget
                widgetManager.updateAppWidget(appWidgetId, views);
            }
        }

    } catch (Exception eee) {
        Log.e(TAG, "Unable to update widget(s)", eee);
    }

}

From source file:com.master.metehan.filtereagle.ServiceSinkhole.java

public static void setPcap(boolean enabled, Context context) {
    File pcap = (enabled ? new File(context.getCacheDir(), "netguard.pcap") : null);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String r = prefs.getString("pcap_record_size", null);
    if (TextUtils.isEmpty(r))
        r = "64";
    String f = prefs.getString("pcap_file_size", null);
    if (TextUtils.isEmpty(f))
        f = "2";/*from  ww w.  ja  va 2  s.com*/
    int record_size = Integer.parseInt(r);
    int file_size = Integer.parseInt(f) * 1024 * 1024;
    jni_pcap(pcap == null ? null : pcap.getAbsolutePath(), record_size, file_size);
}

From source file:cn.org.eshow.framwork.http.AbHttpClient.java

/**
 * ?./*  w  ww  . j a  va  2 s  .  c om*/
 *
 * @param context the context
 */
public AbHttpClient(Context context) {
    mContext = context;
    mExecutorService = AbThreadFactory.getExecutorService();
    mHttpContext = new BasicHttpContext();
    cacheMaxAge = AbAppConfig.DISK_CACHE_EXPIRES_TIME;
    PackageInfo info = AbAppUtil.getPackageInfo(context);
    File cacheDir = null;
    if (!AbFileUtil.isCanUseSD()) {
        cacheDir = new File(context.getCacheDir(), info.packageName);
    } else {
        cacheDir = new File(AbFileUtil.getCacheDownloadDir(context));
    }
    this.httpCache = AbHttpBaseCache.getInstance();
    this.diskCache = new AbDiskBaseCache(cacheDir);
}

From source file:com.sogeti.droidnetworking.NetworkEngine.java

public void init(final Context context, final Map<String, String> headers) {
    this.context = context;

    // Setup a queue for operations
    sharedNetworkQueue = Executors.newFixedThreadPool(2);

    // Init the memory cache, if the default memory cache size shouldn't be used, set the
    // size using setMemoryCacheSize before calling init
    if (memoryCacheSize > 0) {
        memoryCache = new LruCache<String, CacheEntry>(memoryCacheSize) {
            protected int sizeOf(final String key, final CacheEntry entry) {
                return entry.size();
            }/*from w  w w .ja va2 s.co  m*/
        };
    } else {
        memoryCache = null;
    }

    // Init the disk cache, if the default disk cache size shouldn't be used, set the
    // size using setDiskCacheSize before calling init
    if (diskCacheSize > 0) {
        try {
            diskCache = DiskLruCache.open(context.getCacheDir(), DISK_CACHE_VERSION, DISK_CACHE_VALUE_COUNT,
                    diskCacheSize);
        } catch (IOException e) {
            diskCache = null;
        }
    } else {
        diskCache = null;
    }

    // Setup HTTP
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), httpPort));

    // Setup HTTPS (accept all certificates)
    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
    schemeRegistry.register(new Scheme("https", socketFactory, httpsPort));

    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
    HttpConnectionParams.setSoTimeout(params, socketTimeout);

    ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(params, schemeRegistry);

    httpClient = new DefaultHttpClient(connManager, params);

    if (headers == null) {
        this.headers = new HashMap<String, String>();
    } else {
        this.headers = headers;
    }

    if (!this.headers.containsKey("User-Agent")) {
        try {
            PackageInfo packageInfo = this.context.getPackageManager()
                    .getPackageInfo(this.context.getPackageName(), 0);
            this.headers.put("User-Agent", packageInfo.packageName + "/" + packageInfo.versionName);
        } catch (NameNotFoundException e) {
            this.headers.put("User-Agent", "Unknown/0.0");
        }
    }
}

From source file:com.colorchen.qbase.utils.FileUtil.java

/**
 * ?/*from  w  ww.  jav a2  s .  c  o m*/
 *
 * @param context
 * @return
 */
public static String getDiskCacheDir(Context context) {
    String cachePath = null;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    return cachePath;
}

From source file:com.remobile.file.FileUtils.java

private JSONObject requestAllPaths() throws JSONException {
    Context context = cordova.getActivity();
    JSONObject ret = new JSONObject();
    ret.put("applicationDirectory", "file:///android_asset/");
    ret.put("applicationStorageDirectory", toDirUrl(context.getFilesDir().getParentFile()));
    ret.put("dataDirectory", toDirUrl(context.getFilesDir()));
    ret.put("cacheDirectory", toDirUrl(context.getCacheDir()));
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        try {//  w w w  . ja v a  2  s.  co  m
            ret.put("externalApplicationStorageDirectory",
                    toDirUrl(context.getExternalFilesDir(null).getParentFile()));
            ret.put("externalDataDirectory", toDirUrl(context.getExternalFilesDir(null)));
            ret.put("externalCacheDirectory", toDirUrl(context.getExternalCacheDir()));
            ret.put("externalRootDirectory", toDirUrl(Environment.getExternalStorageDirectory()));
        } catch (NullPointerException e) {
            /* If external storage is unavailable, context.getExternal* returns null */
            Log.d(LOG_TAG, "Unable to access these paths, most liklely due to USB storage");
        }
    }
    return ret;
}

From source file:com.remobile.file.FileUtils.java

protected HashMap<String, String> getAvailableFileSystems(Activity activity) {
    Context context = activity.getApplicationContext();
    HashMap<String, String> availableFileSystems = new HashMap<String, String>();

    availableFileSystems.put("files", context.getFilesDir().getAbsolutePath());
    availableFileSystems.put("documents", new File(context.getFilesDir(), "Documents").getAbsolutePath());
    availableFileSystems.put("cache", context.getCacheDir().getAbsolutePath());
    availableFileSystems.put("root", "/");
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        try {//from  w  w  w .j  ava2  s . c  o m
            availableFileSystems.put("files-external", context.getExternalFilesDir(null).getAbsolutePath());
            availableFileSystems.put("sdcard", Environment.getExternalStorageDirectory().getAbsolutePath());
            availableFileSystems.put("cache-external", context.getExternalCacheDir().getAbsolutePath());
        } catch (NullPointerException e) {
            Log.d(LOG_TAG, "External storage unavailable, check to see if USB Mass Storage Mode is on");
        }
    }

    return availableFileSystems;
}