Example usage for android.support.v4.content ContextCompat getNoBackupFilesDir

List of usage examples for android.support.v4.content ContextCompat getNoBackupFilesDir

Introduction

In this page you can find the example usage for android.support.v4.content ContextCompat getNoBackupFilesDir.

Prototype

public final File getNoBackupFilesDir(Context context) 

Source Link

Usage

From source file:org.acra.collector.LogFileCollector.java

/**
 * guess the application log file location and open it
 *
 * @param directory the base directory for the file path
 * @param fileName the name of the file//from  w  w  w.  jav  a  2 s  . c  o  m
 * @return a stream to the file or an empty stream if the file was not found
 */
@NonNull
private InputStream getStream(@NonNull Directory directory, @NonNull String fileName) {
    if (directory == Directory.FILES_LEGACY) {
        directory = fileName.startsWith("/") ? Directory.ROOT : Directory.FILES;
    }
    final File dir;
    switch (directory) {
    case FILES:
        dir = context.getFilesDir();
        break;
    case EXTERNAL_FILES:
        dir = context.getExternalFilesDir(null);
        break;
    case CACHE:
        dir = context.getCacheDir();
        break;
    case EXTERNAL_CACHE:
        dir = context.getExternalCacheDir();
        break;
    case NO_BACKUP_FILES:
        dir = ContextCompat.getNoBackupFilesDir(context);
        break;
    case EXTERNAL_STORAGE:
        dir = Environment.getExternalStorageDirectory();
        break;
    case ROOT:
    default:
        dir = new File("/");
        break;
    }
    final File file = new File(dir, fileName);
    if (!file.exists()) {
        if (ACRA.DEV_LOGGING)
            ACRA.log.d(LOG_TAG, "Log file '" + file.getPath() + "' does not exist");
    } else if (file.isDirectory()) {
        ACRA.log.e(LOG_TAG, "Log file '" + file.getPath() + "' is a directory");
    } else if (!file.canRead()) {
        ACRA.log.e(LOG_TAG, "Log file '" + file.getPath() + "' can't be read");
    } else {
        try {
            return new FileInputStream(file);
        } catch (IOException e) {
            ACRA.log.e(LOG_TAG, "Could not open stream for log file '" + file.getPath() + "'");
        }
    }
    return new ByteArrayInputStream(new byte[0]);
}