android.content.Context.getFilesDir()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(6.2k)|赞(0)|评价(0)|浏览(818)

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

Context.getFilesDir介绍

暂无

代码示例

代码示例来源:origin: square/leakcanary

private File appStorageDirectory() {
 File appFilesDirectory = context.getFilesDir();
 return new File(appFilesDirectory, "leakcanary");
}

代码示例来源:origin: facebook/stetho

private static File getBaseDir(Context context) {
 // getFilesDir() yields /data/data/<package>/files, we want the base package dir.
 return context.getFilesDir().getParentFile();
}

代码示例来源:origin: pockethub/PocketHub

@Provides
  @Named("cacheDir")
  File cacheDir(Context context) {
    return new File(context.getFilesDir(), "cache");
  }
}

代码示例来源:origin: lingochamp/FileDownloader

public static File getConvertedMarkedFile(final Context context) {
  return new File(context.getFilesDir().getAbsolutePath() + File.separator
      + INTERNAL_DOCUMENT_NAME, OLD_FILE_CONVERTED_FILE_NAME);
}

代码示例来源:origin: ACRA/acra

@NonNull
  @Override
  public File getFile(@NonNull Context context, @NonNull String fileName) {
    return new File(context.getFilesDir(), fileName);
  }
},

代码示例来源:origin: pockethub/PocketHub

private static File getFile(final Context context, final User organization) {
  return new File(context.getFilesDir(), "recent-repos-"
      + organization.id() + ".ser");
}

代码示例来源:origin: facebook/stetho

public void cleanupFiles() {
 for (File file : mContext.getFilesDir().listFiles()) {
  if (file.getName().startsWith(FILENAME_PREFIX)) {
   if (!file.delete()) {
    LogRedirector.w(TAG, "Failed to delete " + file.getAbsolutePath());
   }
  }
 }
 LogRedirector.i(TAG, "Cleaned up temporary network files.");
}

代码示例来源:origin: commonsguy/cw-omnibus

@Override
protected long getDataLength(Uri uri) {
 File f=new File(getContext().getFilesDir(), uri.getPath());
 return(f.length());
}

代码示例来源:origin: commonsguy/cw-omnibus

@Override
protected long getDataLength(Uri uri) {
 File f=new File(getContext().getFilesDir(), uri.getPath());
 return(f.length());
}

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

public static String getRootDirPath(Context context) {
  if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
    File file = ContextCompat.getExternalFilesDirs(context.getApplicationContext(), null)[0];
    return file.getAbsolutePath();
  } else {
    return context.getApplicationContext().getFilesDir().getAbsolutePath();
  }
}

代码示例来源:origin: markzhai/AndroidPerformanceMonitor

static String getPath() {
  String state = Environment.getExternalStorageState();
  String logPath = BlockCanaryInternals.getContext()
      == null ? "" : BlockCanaryInternals.getContext().providePath();
  if (Environment.MEDIA_MOUNTED.equals(state)
      && Environment.getExternalStorageDirectory().canWrite()) {
    return Environment.getExternalStorageDirectory().getPath() + logPath;
  }
  return getContext().provideContext().getFilesDir() + BlockCanaryInternals.getContext().providePath();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void openFileOutput_shouldReturnAFileOutputStream() throws Exception {
 File file = new File("__test__");
 String fileContents = "blah";
 try (FileOutputStream fileOutputStream = context.openFileOutput("__test__", Context.MODE_PRIVATE)) {
  fileOutputStream.write(fileContents.getBytes(UTF_8));
 }
 try (FileInputStream fileInputStream = new FileInputStream(new File(context.getFilesDir(), file.getName()))) {
  byte[] readBuffer = new byte[fileContents.length()];
  fileInputStream.read(readBuffer);
  assertThat(new String(readBuffer, UTF_8)).isEqualTo(fileContents);
 }
}

代码示例来源:origin: robolectric/robolectric

@Override
 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
  final File file =
    new File(ApplicationProvider.getApplicationContext().getFilesDir(), "test_file");
  try {
   file.createNewFile();
  } catch (IOException e) {
   throw new RuntimeException("error creating new file", e);
  }
  return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void openFileInput_shouldReturnAFileInputStream() throws Exception {
 String fileContents = "blah";
 File file = new File(context.getFilesDir(), "__test__");
 try (Writer fileWriter = Files.newBufferedWriter(file.toPath(), UTF_8)) {
  fileWriter.write(fileContents);
 }
 try (FileInputStream fileInputStream = context.openFileInput("__test__")) {
  byte[] bytes = new byte[fileContents.length()];
  fileInputStream.read(bytes);
  assertThat(bytes).isEqualTo(fileContents.getBytes(UTF_8));
 }
}

代码示例来源:origin: robolectric/robolectric

@Before
public void setUp() throws Exception {
 file = new File(ApplicationProvider.getApplicationContext().getFilesDir(), "test");
 FileOutputStream os = new FileOutputStream(file);
 os.close();
 readOnlyFile =
   new File(ApplicationProvider.getApplicationContext().getFilesDir(), "test_readonly");
 os = new FileOutputStream(readOnlyFile);
 os.write(READ_ONLY_FILE_CONTENTS);
 os.close();
 assertThat(readOnlyFile.setReadOnly()).isTrue();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void getFilesDir_shouldCreateDirectory() throws Exception {
 assertThat(context.getFilesDir().exists()).isTrue();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void deleteFile_shouldReturnFalse() throws IOException {
 File filesDir = context.getFilesDir();
 File file = new File(filesDir, "test.txt");
 boolean successfully = context.deleteFile(file.getName());
 assertThat(successfully).isFalse();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void deleteFile_shouldReturnTrue() throws IOException {
 File filesDir = context.getFilesDir();
 File file = new File(filesDir, "test.txt");
 boolean successfully = file.createNewFile();
 assertThat(successfully).isTrue();
 successfully = context.deleteFile(file.getName());
 assertThat(successfully).isTrue();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void fileList() throws Exception {
 assertThat(context.fileList()).isEqualTo(context.getFilesDir().list());
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testAutoCloseOutputStream() throws Exception {
 File f = new File(ApplicationProvider.getApplicationContext().getFilesDir(), "outfile");
 ParcelFileDescriptor pfd = ParcelFileDescriptor.open(f, -1);
 ParcelFileDescriptor.AutoCloseOutputStream os =
   new ParcelFileDescriptor.AutoCloseOutputStream(pfd);
 os.close();
 assertThat(pfd.getFileDescriptor().valid()).isFalse();
}

相关文章

Context类方法