Java Code Examples for android.content.Context#getFilesDir()

The following examples show how to use android.content.Context#getFilesDir() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileUtils.java    From clevertap-android-sdk with MIT License 6 votes vote down vote up
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());
    }
}
 
Example 2
Source File: FileManager.java    From HPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 保存
 */
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;
}
 
Example 3
Source File: Storage.java    From ucar-weex-core with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 4
Source File: FileUtils.java    From Kalle with Apache License 2.0 5 votes vote down vote up
public static File getAppRootPath(Context context) {
    if (sdCardIsAvailable()) {
        return Environment.getExternalStorageDirectory();
    } else {
        return context.getFilesDir();
    }
}
 
Example 5
Source File: EnvironmentUtils.java    From zone-sdk with MIT License 5 votes vote down vote up
/**
 * 这里的文件夹已经被创建过了
 *
 * @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;
}
 
Example 6
Source File: ScreenShoot.java    From Utils with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: JobStore.java    From JobSchedulerCompat with Apache License 2.0 5 votes vote down vote up
public static JobStore initAndGet(Context context) {
    synchronized (sSingletonLock) {
        if (sSingleton == null) {
            sSingleton = new JobStore(context, context.getFilesDir());
        }
        return sSingleton;
    }
}
 
Example 8
Source File: FileResourceUtil.java    From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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;
}
 
Example 9
Source File: CrashHandler.java    From AndroidStudyDemo with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 获取错误报告文件名
 * @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);
}
 
Example 10
Source File: FileUtils.java    From AndPermission with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 11
Source File: TestNativeReportFilesProvider.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 12
Source File: GetKeystoreWalletRepoTest.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
@Before
public void setUp() {
	Context context = InstrumentationRegistry.getTargetContext();
	accountKeystoreService = new KeystoreAccountService(new File(context.getFilesDir(), "store"),
														new File(context.getFilesDir(), ""),
														new KeyService(null));
}
 
Example 13
Source File: AccountManager.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
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++;
    }
}
 
Example 14
Source File: StorageUtils.java    From XposedSmsCode with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取日志路径
 */
public static File getLogDir(Context context) {
    if (isSDCardMounted()) {
        return context.getExternalFilesDir("log");
    } else {
        return new File(context.getFilesDir(), "log");
    }
}
 
Example 15
Source File: Settings.java    From PingHeart with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 16
Source File: DictionaryInfoUtils.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method to get the top level cache directory.
 */
private static String getWordListCacheDirectory(final Context context) {
    return context.getFilesDir() + File.separator + "dicts";
}
 
Example 17
Source File: Paths.java    From YalpStore with GNU General Public License v2.0 4 votes vote down vote up
static public File getFilesDir(Context context) {
    if (null == filesDir) {
        filesDir = context.getFilesDir();
    }
    return filesDir;
}
 
Example 18
Source File: FileUtil.java    From diycode with Apache License 2.0 4 votes vote down vote up
/**
 * @param context    上下文
 * @param customPath 自定义路径
 * @return 程序系统文件目录绝对路径
 */
public static String getFileDir(Context context, String customPath) {
    String path = context.getFilesDir() + formatPath(customPath);
    mkdir(path);
    return path;
}
 
Example 19
Source File: Book.java    From BookyMcBookface with GNU General Public License v3.0 4 votes vote down vote up
Book(Context context) {
    this.dataDir = context.getFilesDir();
    this.context = context;
    sectionIDs = new ArrayList<>();
}
 
Example 20
Source File: FileResourcesUtil.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static File getUploadDirectory(Context context) {
    File uploadDirectory = new File(context.getFilesDir(), "upload");
    if (!uploadDirectory.exists())
        uploadDirectory.mkdirs();
    return uploadDirectory;
}