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

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

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

Activity.getCacheDir介绍

暂无

代码示例

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

private File getTempPhotoStagingDirectory() {
  File photoDir = new File(getActivity().getCacheDir(), "photoFiles");
  photoDir.mkdirs();
  return photoDir;
}

代码示例来源:origin: journeyapps/zxing-android-embedded

/**
 * Save the barcode image to a temporary file stored in the application's cache, and return its path.
 * Only does so if returnBarcodeImagePath is enabled.
 *
 * @param rawResult the BarcodeResult, must not be null
 * @return the path or null
 */
private String getBarcodeImagePath(BarcodeResult rawResult) {
  String barcodeImagePath = null;
  if (returnBarcodeImagePath) {
    Bitmap bmp = rawResult.getBitmap();
    try {
      File bitmapFile = File.createTempFile("barcodeimage", ".jpg", activity.getCacheDir());
      FileOutputStream outputStream = new FileOutputStream(bitmapFile);
      bmp.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
      outputStream.close();
      barcodeImagePath = bitmapFile.getAbsolutePath();
    } catch (IOException e) {
      Log.w(TAG, "Unable to create temporary file and store bitmap! " + e);
    }
  }
  return barcodeImagePath;
}

代码示例来源:origin: aa112901/remusic

@Override
  public void onClick(View v) {
    MusicPlayer.clearQueue();
    MusicPlayer.stop();
    File file = new File(mContext.getCacheDir().getAbsolutePath() + "playlist");
    if (file.exists()) {
      file.delete();
    }
    MusicPlaybackState.getInstance(mContext).clearQueue();
    if (adapter != null)
      adapter.notifyDataSetChanged();
    dismiss();
  }
});

代码示例来源:origin: WVector/AppUpdate

path = getActivity().getCacheDir().getAbsolutePath();

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

protected File createTempFileFromAsset(String assetPath) throws IOException {
  InputStream inputStream = null;
  FileOutputStream outStream = null;
  try {
    AssetManager assets = getActivity().getResources().getAssets();
    inputStream = assets.open(assetPath);
    File outputDir = getActivity().getCacheDir(); // context being the Activity pointer
    File outputFile = File.createTempFile("prefix", assetPath, outputDir);
    outStream = new FileOutputStream(outputFile);
    final int bufferSize = 1024 * 2;
    byte[] buffer = new byte[bufferSize];
    int n = 0;
    while ((n = inputStream.read(buffer)) != -1) {
      outStream.write(buffer, 0, n);
    }
    return outputFile;
  } finally {
    Utility.closeQuietly(outStream);
    Utility.closeQuietly(inputStream);
  }
}

代码示例来源:origin: com.uphyca/android-junit4-robolectric

/**
 * @return
 * @see android.content.ContextWrapper#getCacheDir()
 */
public File getCacheDir() {
  return mActivity.getCacheDir();
}

代码示例来源:origin: enricocid/iven-feed-reader

public void clearApplicationData() {
  File cache = getActivity().getCacheDir();
  File appDir = new File(cache.getParent());
  if (appDir.exists()) {
    String[] children = appDir.list();
    for (String s : children) {
      if (!s.equals("lib")) {
        deleteDir(new File(appDir, s));
      }
    }
  }
}

代码示例来源:origin: HelloChenJinJun/TestChat

public static Uri buildUri(Activity activity) {
        if (CommonUtils.isSupportSdcard()) {
            return Uri.fromFile(Environment.getExternalStorageDirectory()).buildUpon().appendPath(CROP_NAME).build();
        } else {
            return Uri.fromFile(activity.getCacheDir()).buildUpon().appendPath(CROP_NAME).build();
        }
    }
}

代码示例来源:origin: woxingxiao/GracefulMovies

private boolean clearCache() {
  return deleteDir(new File(getActivity().getCacheDir().getAbsolutePath()));
}

代码示例来源:origin: sealtalk/sealtalk-android

/**
 * 构建uri
 *
 * @param activity
 * @return
 */
private Uri buildUri(Activity activity) {
  if (CommonUtils.checkSDCard()) {
    return Uri.fromFile(Environment.getExternalStorageDirectory()).buildUpon().appendPath(CROP_FILE_NAME).build();
  } else {
    return Uri.fromFile(activity.getCacheDir()).buildUpon().appendPath(CROP_FILE_NAME).build();
  }
}

代码示例来源:origin: j4velin/MapsMeasure

@Override
  public void onClick(final View v) {
    try {
      final File f = new File(c.getCacheDir(), "MapsMeasure.csv");
      Util.saveToFile(f, trace);
      Intent shareIntent = new Intent();
      shareIntent.setAction(Intent.ACTION_SEND);
      shareIntent.putExtra(Intent.EXTRA_STREAM, FileProvider
          .getUriForFile(c, "de.j4velin.mapsmeasure.fileprovider", f));
      shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      shareIntent.setType("text/comma-separated-values");
      d.dismiss();
      c.startActivity(Intent.createChooser(shareIntent, null));
    } catch (IOException e) {
      if (BuildConfig.DEBUG) Logger.log(e);
      e.printStackTrace();
      Toast.makeText(c, c.getString(R.string.error,
          e.getClass().getSimpleName() + "\n" + e.getMessage()),
          Toast.LENGTH_LONG).show();
    }
  }
});

代码示例来源:origin: woxingxiao/GracefulMovies

private String getCacheSizeString() {
  long size = getFolderSize(new File(getActivity().getCacheDir().getAbsolutePath()));
  return formatFileSize(size);
}

代码示例来源:origin: Swati4star/Images-to-PDF

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
  if (result == null || result.getContents() == null)
    showSnackbar(mActivity, R.string.scan_cancelled);
  else {
    Toast.makeText(mActivity, " " + result.getContents(), Toast.LENGTH_SHORT).show();
    File mDir = mActivity.getCacheDir();
    File mTempFile = new File(mDir.getPath() + "/" + mTempFileName);
    PrintWriter mWriter;
    try {
      mWriter = new PrintWriter(mTempFile);
      mWriter.print("");
      mWriter.append(result.getContents());
      mWriter.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    Uri uri = Uri.fromFile(mTempFile);
    resultToTextPdf(uri);
  }
}

代码示例来源:origin: stackoverflow.com

File fileTmp = activity.getCacheDir(); 
File fileAppRoot = new File(activity.getApplicationInfo().dataDir);

代码示例来源:origin: blurpy/kouchat-android

/**
 * Copies the file <code>kouchat-1600x1600.png</code> from assets to the cache directory of the internal storage,
 * if it's not already there. It will not be added to the media database.
 *
 * <p>The internal storage should be available even if the SD card is unmounted.</p>
 *
 * @param instrumentation Test instrumentation.
 * @param activity The activity under test.
 */
public static void copyKouChatImageFromAssetsToInternalStorage(final Instrumentation instrumentation, final Activity activity) {
  final File cacheDir = activity.getCacheDir();
  copyKouChatImageFromAssetsToStorage(instrumentation, activity, cacheDir, false);
}

代码示例来源:origin: materialos/android-icon-pack

@Override
public void run() {
  File dest = context.getCacheDir();
  dest.mkdirs();
  dest = new File(dest, icon.getName() + ".png");

代码示例来源:origin: blurpy/kouchat-android

/**
 * Returns a representation of <code>kouchat-1600x1600.png</code> than can be used to get the
 * actual file on the internal storage.
 *
 * @param activity The activity under test.
 * @return <code>kouchat-1600x1600.png</code>.
 */
public static AndroidFile getKouChatImageFromInternalStorage(final Activity activity) {
  final File cacheDir = activity.getCacheDir();
  final File image = new File(cacheDir, KOUCHAT_FILE);
  return new AndroidFile(image);
}

代码示例来源:origin: jclehner/rxdroid

private String createBackupFileFromContentStream(Uri uri)
  {
    try
    {
      final File tempFile = new File(getActivity().getCacheDir(), "temp.rxdbak");
      final InputStream in = getActivity().getContentResolver().openInputStream(uri);
      final OutputStream out = new FileOutputStream(tempFile);
      Util.copy(in, out);
      return tempFile.getAbsolutePath();
    }
    catch(FileNotFoundException e)
    {
      throw new WrappedCheckedException(e);
    }
    catch(IOException e)
    {
      throw new WrappedCheckedException(e);
    }
  }
}

代码示例来源:origin: leeowenowen/AndroidUnitTest

@Before
public void setUp() throws Exception {
  // temporary workaround for an incompatibility in current dexmaker (1.1)
  // implementation and Android >= 4.3
  // cf. https://code.google.com/p/dexmaker/issues/detail?id=2 for details
  System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
  MockitoAnnotations.initMocks(this);
}

代码示例来源:origin: rsiebert/TVHClient

continue;
File file = new File(getActivity().getCacheDir(), MiscUtils.convertUrlToHashString(channel.getIcon()) + ".png");
if (file.exists()) {
  if (!file.delete()) {

相关文章

Activity类方法