下面列出了android.content.Context#getFilesDir ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void deleteFile(Context context, CleverTapInstanceConfig config, String fileName) throws Exception {
if (TextUtils.isEmpty(fileName) || context == null)
return;
try {
File file = new File(context.getFilesDir(), fileName);
if (file.exists()) {
if (file.delete()) {
if (config != null)
config.getLogger().verbose(config.getAccountId(), "File Deleted:" + fileName);
} else {
if (config != null)
config.getLogger().verbose(config.getAccountId(), "Failed to delete file" + fileName);
}
}
} catch (Exception e) {
e.printStackTrace();
if (config != null)
config.getLogger().verbose(config.getAccountId(), "writeFileOnInternalStorage: failed" + fileName + " Error:" + e.getLocalizedMessage());
}
}
/**
* 保存
*/
public static boolean saveData(Context context, Serializable serializable, String fileName) {
if (serializable == null || TextUtils.isEmpty(fileName)) {
return false;
}
try {
File file = new File(context.getFilesDir(), fileName);
if (!file.exists()) {
file.createNewFile();
}
return saveSerializable(serializable, file);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static File getAppRootPath(Context context) {
if (sdCardIsAvailable()) {
return Environment.getExternalStorageDirectory();
} else {
return context.getFilesDir();
}
}
private synchronized static boolean deleteLocalFile(Context context, String filename){
File file = new File(context.getFilesDir(), filename);
boolean success = false;
try {
if (file.exists())
success = deleteFile(file);
} catch (Exception e) {
throw new RuntimeException(e);
}
return success;
}
/**
* 获取日志路径
*/
public static File getLogDir(Context context) {
if (isSDCardMounted()) {
return context.getExternalFilesDir("log");
} else {
return new File(context.getFilesDir(), "log");
}
}
private File getUniqueDbName(Context context) {
File dir = context.getFilesDir();
int index = 1;
while (true) {
File test = new File(dir, String.format("messenger-%d.db", index));
File testBlobdir = new File(dir, String.format("messenger-%d.db-blobs", index));
if (!test.exists() && !testBlobdir.exists()) {
return test;
}
index++;
}
}
@Before
public void setUp() {
Context context = InstrumentationRegistry.getTargetContext();
accountKeystoreService = new KeystoreAccountService(new File(context.getFilesDir(), "store"),
new File(context.getFilesDir(), ""),
new KeyService(null));
}
public TestNativeReportFilesProvider(Context context) {
final File filesDir = context.getFilesDir();
recursiveDelete(filesDir);
File rootFolder = new File(context.getFilesDir(), UUID.randomUUID().toString());
rootFolder.mkdirs();
validFiles = new File(rootFolder, UUID.randomUUID().toString());
validFiles.mkdirs();
}
public static File getFileDir(Context context, @Nullable String type) {
File root = context.getFilesDir();
if (TextUtils.isEmpty(type)) {
return root;
} else {
File dir = new File(root, type);
createDir(dir);
return dir;
}
}
/**
* 获取错误报告文件名
* @param context
* @return
*/
private String[] getCrashReportFiles(Context context) {
File filesDir = context.getFilesDir();
// 实现FilenameFilter接口的类实例可用于过滤器文件名
FilenameFilter filter = new FilenameFilter() {
// accept(File dir, String name)
// 测试指定文件是否应该包含在某一文件列表中。
public boolean accept(File dir, String name) {
return name.endsWith(CRASH_REPORTER_EXTENSION);
}
};
// list(FilenameFilter filter)
// 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中满足指定过滤器的文件和目录
return filesDir.list(filter);
}
static File renameFile(File file, String newFileName, Context context) {
String contentType = URLConnection.guessContentTypeFromName(file.getName());
String generatedName = generateFileName(MediaType.get(contentType), newFileName);
File newFile = new File(context.getFilesDir(), "sdk_resources/" + generatedName);
if (!file.renameTo(newFile)) {
Log.d(FileResourceUtil.class.getCanonicalName(),
"Fail renaming " + file.getName() + " to " + generatedName);
}
return newFile;
}
public static JobStore initAndGet(Context context) {
synchronized (sSingletonLock) {
if (sSingleton == null) {
sSingleton = new JobStore(context, context.getFilesDir());
}
return sSingleton;
}
}
/**
* generate the default path for the file.
* default file name format is: yyyyMMddHHmmss.png.
*/
private static String getDefaultPath(Context context) {
String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".png";
String path = context.getFilesDir() + fileName;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
path = context.getExternalCacheDir() + File.separator + fileName;
}
return path;
}
/**
* 这里的文件夹已经被创建过了
*
* @param context
* @param ignoreSDRemovable 可移除的sd是否无视
* @return
*/
public static File getSmartFilesDir(Context context, boolean ignoreSDRemovable) {
//SD卡 存在 并且不是 拔插的
File fileResult=context.getFilesDir();
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
&&(ignoreSDRemovable || Environment.isExternalStorageRemovable())){
File fileTemp= context.getExternalFilesDir(null);
if(fileTemp!=null)
fileResult=fileTemp;//导致即使不为Null的可能
}
return fileResult;
}
@TargetApi(8)
private static File getExternalFilesDir(Context context) {
if (hasFroyo()) {
File dir1 = context.getExternalFilesDir((String) null);
return dir1 != null ? dir1 : context.getFilesDir();
} else {
String dir = "/Android/content/" + context.getPackageName() + "/files";
return new File(Environment.getExternalStorageDirectory(), dir);
}
}
public static File getUploadDirectory(Context context) {
File uploadDirectory = new File(context.getFilesDir(), "upload");
if (!uploadDirectory.exists())
uploadDirectory.mkdirs();
return uploadDirectory;
}
Book(Context context) {
this.dataDir = context.getFilesDir();
this.context = context;
sectionIDs = new ArrayList<>();
}
/**
* Helper method to get the top level cache directory.
*/
private static String getWordListCacheDirectory(final Context context) {
return context.getFilesDir() + File.separator + "dicts";
}
static public File getFilesDir(Context context) {
if (null == filesDir) {
filesDir = context.getFilesDir();
}
return filesDir;
}
/**
* @param context 上下文
* @param customPath 自定义路径
* @return 程序系统文件目录绝对路径
*/
public static String getFileDir(Context context, String customPath) {
String path = context.getFilesDir() + formatPath(customPath);
mkdir(path);
return path;
}