android.app.Application.getCacheDir()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(104)

本文整理了Java中android.app.Application.getCacheDir()方法的一些代码示例,展示了Application.getCacheDir()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Application.getCacheDir()方法的具体详情如下:
包路径:android.app.Application
类名称:Application
方法名:getCacheDir

Application.getCacheDir介绍

暂无

代码示例

代码示例来源:origin: android10/Android-CleanArchitecture

public static File cacheDir() {
  return RuntimeEnvironment.application.getCacheDir();
 }
}

代码示例来源:origin: facebook/facebook-android-sdk

public static File openAttachment(UUID callId, String attachmentName)
      throws FileNotFoundException {
    if (attachments.contains(new Pair<>(callId, attachmentName))) {
      File cacheDir = RuntimeEnvironment.application.getCacheDir();
      File dummyFile = new File(cacheDir, DUMMY_FILE_NAME);
      if (!dummyFile.exists()) {
        try {
          dummyFile.createNewFile();
        } catch (IOException e) {
        }
      }
      return dummyFile;
    }
    throw new FileNotFoundException();
  }
}

代码示例来源:origin: CarGuo/GSYVideoPlayer

private void getDanmu() {
  //下载demo然后设置
  OkHttpUtils.get().url(TextUtils.concat("http://xingyuyou.com/Public/app/barragefile/","608","barrage.txt").toString())
      .build()
      .execute(new FileCallBack(getApplication().getCacheDir().getAbsolutePath(), "barrage.txt")//
      {
        @Override
        public void onError(Call call, Exception e, int id) {
        }
        @Override
        public void onResponse(File response, int id) {
          if (!isDestory) {
            ((DanmakuVideoPlayer) danmakuVideoPlayer.getCurrentPlayer()).setDanmaKuStream(response);
          }
        }
      });
}

代码示例来源:origin: bumptech/glide

@Test
public void testHandlesFileUris() {
 File f = RuntimeEnvironment.application.getCacheDir();
 Uri expected = Uri.fromFile(f);
 when(uriLoader.buildLoadData(eq(expected), eq(IMAGE_SIDE), eq(IMAGE_SIDE), eq(options)))
   .thenReturn(new ModelLoader.LoadData<>(key, fetcher));
 assertTrue(loader.handles(f.getAbsolutePath()));
 assertEquals(
   fetcher,
   Preconditions.checkNotNull(
     loader.buildLoadData(expected.toString(), IMAGE_SIDE, IMAGE_SIDE, options)).fetcher);
}

代码示例来源:origin: bumptech/glide

@Before
public void setUp() {
 dir = RuntimeEnvironment.application.getCacheDir();
 cache = DiskLruCacheWrapper.create(dir, 10 * 1024 * 1024);
 key = new ObjectKey("test" + Math.random());
 data = new byte[] { 1, 2, 3, 4, 5, 6 };
}

代码示例来源:origin: bumptech/glide

@SuppressWarnings("unchecked")
@Before
public void setUp() {
 MockitoAnnotations.initMocks(this);
 Application context = RuntimeEnvironment.application;
 ReEncodingGifResourceEncoder.Factory factory = mock(ReEncodingGifResourceEncoder.Factory.class);
 when(decoder.getNextFrame()).thenReturn(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888));
 when(factory.buildDecoder(any(GifDecoder.BitmapProvider.class))).thenReturn(decoder);
 when(factory.buildParser()).thenReturn(parser);
 when(factory.buildEncoder()).thenReturn(gifEncoder);
 when(factory.buildFrameResource(any(Bitmap.class), any(BitmapPool.class)))
   .thenReturn(frameResource);
 // TODO Util.anyResource once Util is moved to testutil module (remove unchecked above!)
 when(frameTransformation.transform(anyContext(), any(Resource.class), anyInt(), anyInt()))
   .thenReturn(frameResource);
 when(gifDrawable.getFrameTransformation()).thenReturn(frameTransformation);
 when(gifDrawable.getBuffer()).thenReturn(ByteBuffer.allocate(0));
 when(resource.get()).thenReturn(gifDrawable);
 encoder = new ReEncodingGifResourceEncoder(context, mock(BitmapPool.class), factory);
 options = new Options();
 options.set(ReEncodingGifResourceEncoder.ENCODE_TRANSFORMATION, true);
 file = new File(context.getCacheDir(), "test");
}

代码示例来源:origin: k9mail/k-9

@Before
public void setUp() throws Exception {
  BinaryTempFileBody.setTempDirectory(RuntimeEnvironment.application.getCacheDir());
  imapStore = mock(ImapStore.class);
  storeConfig = mock(StoreConfig.class);
  when(storeConfig.getInboxFolder()).thenReturn("INBOX");
  when(imapStore.getCombinedPrefix()).thenReturn("");
  when(imapStore.getStoreConfig()).thenReturn(storeConfig);
  imapConnection = mock(ImapConnection.class);
}

代码示例来源:origin: bumptech/glide

@Test
public void testHandlesPaths() {
 // TODO fix drive letter parsing somehow
 assumeTrue("it will fail with schema being the drive letter (C:\\... -> C)", !Util.isWindows());
 File f = RuntimeEnvironment.application.getCacheDir();
 Uri expected = Uri.fromFile(f);
 when(uriLoader.buildLoadData(eq(expected), eq(IMAGE_SIDE), eq(IMAGE_SIDE), eq(options)))
   .thenReturn(new ModelLoader.LoadData<>(key, fetcher));
 assertTrue(loader.handles(f.getAbsolutePath()));
 assertEquals(
   fetcher,
   Preconditions.checkNotNull(
     loader.buildLoadData(f.getAbsolutePath(), IMAGE_SIDE, IMAGE_SIDE, options)).fetcher);
}

代码示例来源:origin: bumptech/glide

@Before
public void setUp() {
 encoder = new StreamEncoder(new LruArrayPool());
 file = new File(RuntimeEnvironment.application.getCacheDir(), "test");
}

代码示例来源:origin: k9mail/k-9

@Before
public void setUp() throws Exception {
  BinaryTempFileBody.setTempDirectory(RuntimeEnvironment.application.getCacheDir());
}

代码示例来源:origin: palaima/DebugDrawer

private static OkHttpClient.Builder createOkHttpClientBuilder(Application app) {
    // Install an HTTP cache in the application cache directory.
    File cacheDir = new File(app.getCacheDir(), "okhttp3-cache");
    Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);

    return new OkHttpClient.Builder()
      .cache(cache)
      .addInterceptor(LogsModule.chuckInterceptor(app))
      .addInterceptor(NetworkQualityModule.interceptor(app))
      .readTimeout(10, TimeUnit.SECONDS)
      .writeTimeout(10, TimeUnit.SECONDS)
      .connectTimeout(10, TimeUnit.SECONDS);
  }
}

代码示例来源:origin: palaima/DebugDrawer

private static OkHttpClient.Builder createOkHttpClientBuilder(Application app) {
    // Install an HTTP cache in the application cache directory.
    File cacheDir = new File(app.getCacheDir(), "okhttp3");
    Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);

    return new OkHttpClient.Builder()
      .cache(cache)
      .addInterceptor(LogsModule.chuckInterceptor(app))
      .addInterceptor(NetworkQualityModule.interceptor(app))
      .readTimeout(10, TimeUnit.SECONDS)
      .writeTimeout(10, TimeUnit.SECONDS)
      .connectTimeout(10, TimeUnit.SECONDS);
  }
}

代码示例来源:origin: jberkel/sms-backup-plus

@Before
public void setUp() throws Exception {
  initMocks(this);
  BinaryTempFileBody.setTempDirectory(RuntimeEnvironment.application.getCacheDir());
  messageConverter = new MessageConverter(RuntimeEnvironment.application,
      preferences, "foo@example.com", personLookup, contactAccessor);
}

代码示例来源:origin: limpoxe/Android-Plugin-Framework

/**
 * 如果插件中不包含service、receiver,是不需要替换classloader的
 */
public static void hackHostClassLoaderIfNeeded() {
  LogUtil.v("hackHostClassLoaderIfNeeded");
  HackApplication hackApplication = new HackApplication(FairyGlobal.getHostApplication());
  Object mLoadedApk = hackApplication.getLoadedApk();
  if (mLoadedApk == null) {
    //重试一次
    mLoadedApk = hackApplication.getLoadedApk();
  }
  if(mLoadedApk == null) {
    //换个方式再试一次
    mLoadedApk = HackActivityThread.getLoadedApk();
  }
  if (mLoadedApk != null) {
    HackLoadedApk hackLoadedApk = new HackLoadedApk(mLoadedApk);
    ClassLoader originalLoader = hackLoadedApk.getClassLoader();
    if (!(originalLoader instanceof HostClassLoader)) {
      HostClassLoader newLoader = new HostClassLoader("", new RealHostClassLoader("",
          FairyGlobal.getHostApplication().getCacheDir().getAbsolutePath(),/**这里这两个目录参数无实际意义**/
          FairyGlobal.getHostApplication().getCacheDir().getAbsolutePath(),/**这里这两个目录参数无实际意义**/
          originalLoader));
      hackLoadedApk.setClassLoader(newLoader);
    }
  } else {
    LogUtil.e("What!!Why?");
  }
}

代码示例来源:origin: limpoxe/Android-Plugin-Framework

if (!srcPluginFile.startsWith(FairyGlobal.getHostApplication().getCacheDir().getAbsolutePath())) {
  String tempFilePath = FairyGlobal.getHostApplication().getCacheDir().getAbsolutePath()
      + File.separator + System.currentTimeMillis() + "_" + srcFile.getName();
  if (FileUtil.copyFile(srcPluginFile, tempFilePath)) {

代码示例来源:origin: NimbleDroid/FriendlyDemo

@Singleton
@Provides
@ClientCache
File provideCacheFile(Application context)     {
  return new File(context.getCacheDir(), "cache_file");
}

代码示例来源:origin: burgessjp/GanHuoIO

@Override
  public File getCacheDir() {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
      File cacheDir = getExternalCacheDir();
      if (cacheDir != null && (cacheDir.exists() || cacheDir.mkdirs())) {
        return cacheDir;
      }
    }
    return super.getCacheDir();
  }
}

代码示例来源:origin: mingjunli/GithubApp

@Override
public OkHttpClient.Builder customize(OkHttpClient.Builder builder) {
  // set cache dir
  File cacheFile = new File(mContext.getCacheDir(), "github_repo");
  Cache cache = new Cache(cacheFile, CACHE_SIZE);
  builder.cache(cache);
  builder.addNetworkInterceptor(mCacheControlInterceptor);
  return builder;
}

代码示例来源:origin: com.octo.android.robospice/robospice-sample

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  // Initializes the logging
  // Log a message (only on dev platform)
  Ln.i("onCreate");
  loremRequest = new SimpleTextRequest("http://www.loremipsum.de/downloads/original.txt");
  weatherRequest = new WeatherRequestJson("75000");
  weatherRequestXml = new WeatherRequestXml("75000");
  File cacheFile = new File(getApplication().getCacheDir(), "earth.jpg");
  imageRequest = new BigBinaryRequest("http://earthobservatory.nasa.gov/blogs/elegantfigures/files/2011/10/globe_west_2048.jpg", cacheFile);
}

代码示例来源:origin: yshrsmz/LicenseAdapter

@Override
public void run() {
 try {
  library.load(new File(getApplication().getCacheDir(), CACHE_DIR_NAME + File.separator +
    library.getAuthor() + File.separator + library.getName()));
  License license = library.getLicense();
  cachedLicenses.put(library, license);
  notify(license, null);
 } catch (Exception e) {
  notify(library.getLicense(), e);
 }
}

相关文章

微信公众号

最新文章

更多

Application类方法