/** 初始化硬盘缓存 */ private void initDiskCache() { try { String cache; if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) || !Environment.isExternalStorageRemovable()) { if (context.getExternalCacheDir() != null) { cache = context.getExternalCacheDir().getPath(); Log.i("Test", "SD"); } else { throw new Exception("SD卡连接错误"); } } else { cache = context.getCacheDir().getPath(); Log.i("Test", "mobilephone"); } /** SD卡数据缓存目录 */ String dataFileName = "indoor_data"; cache = cache + File.separator + dataFileName; File fileDir = new File(cache); if (!fileDir.exists()) { if (fileDir.mkdirs()) { Log.e("Test", "创建目录: " + fileDir.getAbsolutePath()); } else { Log.e("Test", "创建目录失败"); } } else { Log.e("Test", "目录: " + fileDir.getAbsolutePath()); } diskLruCache = DiskLruCache.open(fileDir, getAppVersion(context), 1, DISK_CACHE_DEFAULT_SIZE); } catch (Exception e) { e.printStackTrace(); } }
/** * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it. * * @param context A {@link Context} to use for creating the cache dir. * @param stack An {@link HttpStack} to use for the network, or null for default. * @return A started {@link RequestQueue} instance. */ public synchronized void init(Context context, HttpStack stack) { File cacheDir = null; String status = Environment.getExternalStorageState(); if (status.equals(Environment.MEDIA_MOUNTED) && context.getExternalCacheDir().exists()) cacheDir = new File(context.getExternalCacheDir(), DEFAULT_CACHE_DIR); else cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR); String userAgent = "com.sohu.focus"; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); userAgent = packageName + "Android/v_" + info.versionCode + ",build/" + Build.VERSION.RELEASE; } catch (NameNotFoundException e) { } if (stack == null) { if (Build.VERSION.SDK_INT >= 9) { stack = new HurlStack(); } else { stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent)); } } diskCache = new DiskBasedCache(cacheDir); Network network = new BasicNetwork(stack); queue = new RequestQueue(diskCache, network); queue.start(); int memoryCacheSize = (int) (Runtime.getRuntime().maxMemory() / 8); memoryCache = new LruMemoryCache(memoryCacheSize); imageLoader = new ImageLoader(queue, memoryCache); }
@SuppressLint("NewApi") public static void prepareCacheFolder(Context context) { if (Util.isFroyoOrLater() && Util.hasExternalStorage() && context.getExternalCacheDir() != null) CACHE_DIR = context.getExternalCacheDir().getPath(); else CACHE_DIR = Environment.getExternalStorageDirectory().getPath() + "/Android/data/" + context.getPackageName() + "/cache"; COVER_DIR = CACHE_DIR + "/covers/"; File file = new File(COVER_DIR); if (!file.exists()) file.mkdirs(); }
/** * Initialize the GuideXML class with the offline (downloaded) version of it * * @param context the app context * @param guideId the guide identifier */ public GuideXML(Context context, String guideId) { // Initialize the class with the offline guide downloaded XML file this( context, guideId, context.getExternalCacheDir() + OFFLINE_GUIDE_PATH + guideId + "/" + guideId + ".xml"); }
public synchronized void pruneCache() { try { final HashSet<Long> currentFiles = new HashSet<Long>(128); final File externalCacheDir = context.getExternalCacheDir(); final File internalCacheDir = context.getCacheDir(); if (externalCacheDir != null) { getCacheFileList(externalCacheDir, currentFiles); } if (internalCacheDir != null) { getCacheFileList(internalCacheDir, currentFiles); } final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); final HashMap<Integer, Long> maxAge = PrefsUtility.pref_cache_maxage(context, prefs); final LinkedList<Long> filesToDelete = dbManager.getFilesToPrune(currentFiles, maxAge, 72); for (final long id : filesToDelete) { fileDeletionQueue.enqueue(id); } } catch (Throwable t) { BugReportActivity.handleGlobalError(context, t); } }
public static File getAppCacheDir(Context context) { File file = context.getExternalCacheDir(); if (file == null) { file = context.getCacheDir(); } return file; }
public BaseCache build(Context context) { BaseCache cache = new BaseCache(); File dir = context.getExternalCacheDir(); if (dir != null && cache.validateLocation(dir)) { cache.mDiskCacheLocation = TextUtils.isEmpty(type) ? dir : new File(dir, type); } else { cache.mDiskCacheLocation = TextUtils.isEmpty(type) ? context.getCacheDir() : new File(context.getCacheDir(), type); } try { cache.mDiskCache = DiskLruCache.open( cache.mDiskCacheLocation, 0, 1, Constants.DEFAULT_DISK_CACHE_MAX_SIZE_MB * Constants.MEGABYTE); } catch (IOException e) { e.printStackTrace(); } cache.mDiskCacheEditLocks = new HashMap<>(); cache.mDiskCacheFlusherExecutor = new ScheduledThreadPoolExecutor(Constants.DEFAULT_DISK_CACHE_MAX_SIZE_MB); cache.mDiskCacheFlusherRunnable = new DiskCacheFlushRunnable(cache.mDiskCache); cache.mTempDir = context.getCacheDir(); return cache; }
public static FileUtils getInstance(Context context) { if (instance == null) { ObbDir = context.getExternalCacheDir().getParentFile().getAbsolutePath(); File file = new File(ObbDir); if (!file.exists()) { file.mkdirs(); } logCache = ObbDir + "/logs"; file = new File(logCache); if (!file.exists()) { file.mkdirs(); } photoCache = ObbDir + "/photos"; file = new File(photoCache); if (!file.exists()) { file.mkdirs(); } fileCache = ObbDir + "/files"; file = new File(fileCache); if (!file.exists()) { file.mkdirs(); } OtherCache = ObbDir + "/files"; file = new File(OtherCache); if (!file.exists()) { file.mkdirs(); } instance = new FileUtils(); } return instance; }
public static String getTotalCacheSize(Context context) throws Exception { long cacheSize = getFolderSize(context.getCacheDir()); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { cacheSize += getFolderSize(context.getExternalCacheDir()); } return getFormatSize(cacheSize); }
/** * 清空临时目录 * * @param context 程序上下文 */ public static void deleteTmpDir(Context context) { // 创建临时文件 File cacheDir = context.getExternalCacheDir(); if (cacheDir.exists()) { if (cacheDir.isDirectory()) for (File child : cacheDir.listFiles()) child.delete(); cacheDir.delete(); } }
/** * 创建文件用于存储临时图片 * * @return 用于存储图片的文件实例 */ public static File getTmpMediaFile(Context context) { // 创建空白目录 String NOMEDIA = ".nomedia"; File noMediaFile = new File(context.getExternalCacheDir().getAbsolutePath() + File.separator + NOMEDIA); if (!noMediaFile.exists()) { try { noMediaFile.createNewFile(); } catch (IOException e) { Log.e(TAG, "无法创建空白文件", e); } } // 创建临时文件 File outputDir = context.getExternalCacheDir(); return getOutputFile(outputDir, null); }
public static String getDiskCacheDir(Context context) { if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) { return context.getExternalCacheDir().getPath(); } else { return context.getCacheDir().getPath(); } }
/** * 有 sdcard 的时候,小米是 /storage/sdcard0/Android/data/com.avoscloud.chat/cache/ 无 sdcard 的时候,小米是 * /data/data/com.avoscloud.chat/cache 依赖于包名。所以不同应用使用该库也没问题,要有点理想。 * * @return */ private static File getAvailableCacheDir(Context context) { if (isExternalStorageWritable()) { return context.getExternalCacheDir(); } else { // 只有此应用才能访问。拍照的时候有问题,因为拍照的应用写入不了该文件 return context.getCacheDir(); } }
/** * Get the external App cache directory. * * @param context The context to use * @return The external cache directory */ public static File getExternalCacheDir(Context context) { if (Utils.hasFroyo()) { return context.getExternalCacheDir(); } // Before Froyo we have to construct the external cache directory ourselves final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/"; return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir); }
@Override protected String getSystemTemporaryDirectory() { File cacheDir = mContext.getExternalCacheDir(); if (cacheDir == null) { mContext.getCacheDir(); mUsingExternalCache = false; } return cacheDir.getAbsolutePath(); }
/** * @param c * @return 返回cache目录 如: /mnt/sdcard/Android/data/com.wasu.app/cache/ */ public static String getExtenalCacheDir(Context c) { File f = c.getExternalCacheDir(); if (f != null) { f.mkdirs(); } else { return null; } return f.getAbsolutePath() + File.separator; }
public static String getCacheFileName(Context context, String url) { String hashedUrl = md5(url); File storage = context.getExternalCacheDir(); File file = new File(storage.getAbsolutePath() + CACHE_PATH + "/" + hashedUrl + ".png"); return file.getAbsolutePath(); }
public static void clearAppCache(Context cxt) { // 清除帐号信息 AccountUtils.removeAll(cxt); // 清除数据缓存 clearCacheFolder(cxt.getFilesDir(), System.currentTimeMillis()); clearCacheFolder(cxt.getCacheDir(), System.currentTimeMillis()); clearCacheFolder(cxt.getExternalCacheDir(), System.currentTimeMillis()); }
/** * Get the external app cache directory. * * @param context The context to use * @return The external cache dir */ @TargetApi(8) public static File getExternalCacheDir(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { return context.getExternalCacheDir(); } } return null; }
/** * Get the external app cache directory. * * @param context The context to use * @return The external cache dir */ @SuppressLint("NewApi") public static File getExternalCacheDir(Context context) { if (hasExternalCacheDir()) { return context.getExternalCacheDir(); } // Before Froyo we need to construct the external cache dir ourselves final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/"; return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir); }
public static File getDiskCacheDir(Context context, String uniqueName) { String cachePath; if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) { cachePath = context.getExternalCacheDir().getPath(); } else { cachePath = context.getCacheDir().getPath(); } return new File(cachePath + File.separator + uniqueName); }
/** * Create file cache. * * @param context */ public FileCache(Context context) { if (android.os.Environment.getExternalStorageState() .equals(android.os.Environment.MEDIA_MOUNTED)) cacheDir = new File(context.getExternalCacheDir(), "inet_img"); else cacheDir = context.getCacheDir(); Log.i(TAG, "FileCache will use folder " + cacheDir.getAbsolutePath()); if (!cacheDir.exists()) { if (cacheDir.mkdirs()) Log.i(TAG, "FileCache folder " + cacheDir.getAbsolutePath() + " created"); } }
public String getUpdateXlsPath(Context mContext) { try { return mContext.getExternalCacheDir().getAbsolutePath(); } catch (Exception e) { try { return Environment.getExternalStorageDirectory().getAbsolutePath(); } catch (Exception e2) { return mContext.getFilesDir().getAbsolutePath(); } } }
public static String getTotalCacheSize(Context context) { long cacheSize = 0; try { File cacheDir = context.getCacheDir(); if (cacheDir == null) { cacheSize = getFolderSize(cacheDir); } if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File file = context.getExternalCacheDir(); if (file != null) { cacheSize += getFolderSize(context.getExternalCacheDir()); Log.i("sys", context.getExternalCacheDir().toString()); } } } catch (Exception e) { e.printStackTrace(); } return getFormatSize(cacheSize); }
/** * 将bitmap转为file * * @param bmp * @param filename * @return */ public static boolean saveBitmap2file(Context context, Bitmap bmp, String filename) { CompressFormat format = Bitmap.CompressFormat.JPEG; int quality = 100; OutputStream stream = null; try { stream = new FileOutputStream(context.getExternalCacheDir().getAbsolutePath() + filename); } catch (FileNotFoundException e) { e.printStackTrace(); } return bmp.compress(format, quality, stream); }
public File getCacheDir() { if (mWebImageDir == null) { File baseDir = mContext.getExternalCacheDir(); if (baseDir == null) { baseDir = new File(mContext.getFilesDir(), "cache"); } baseDir.mkdirs(); mWebImageDir = new File(baseDir, "webimages"); } checkTerminalPath(mWebImageDir); return mWebImageDir; }
public static long getCacheSize(Context cxt) { long fileSize = 0; File filesDir = cxt.getFilesDir(); File cacheDir = cxt.getCacheDir(); File externalCacheDir = cxt.getExternalCacheDir(); fileSize += getDirSize(filesDir); fileSize += getDirSize(cacheDir); fileSize += getDirSize(externalCacheDir); return fileSize; }
public static File getCacheStorageDirectory(boolean allowExternal) { File directory; Context context = MyApplication.getAppContext(); if (allowExternal) { directory = context.getExternalCacheDir(); if (directory == null) { directory = context.getCacheDir(); } } else directory = context.getCacheDir(); return directory; }
@TargetApi(8) public static File getExternalCacheDir(Context context) { File cacheDir = null; if (hasExternalCacheDir()) { cacheDir = context.getExternalCacheDir(); if (cacheDir == null) { String str = "/Android/data/" + context.getPackageName() + "/cache/"; cacheDir = new File(Environment.getExternalStorageDirectory().getPath() + str); } } return cacheDir; }
public void saveLogcatToFile(Context context) { String fileName = "logcat_" + System.currentTimeMillis() + ".txt"; File outputFile = new File(context.getExternalCacheDir(), fileName); activityHandler.fireToast(outputFile.getPath()); try { @SuppressWarnings("unused") Process process = Runtime.getRuntime().exec("logcat -f " + outputFile.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } Log.d("SAVE LOGCAT", "TO: " + outputFile.getAbsolutePath()); }