Example usage for android.content ContextWrapper getDir

List of usage examples for android.content ContextWrapper getDir

Introduction

In this page you can find the example usage for android.content ContextWrapper getDir.

Prototype

@Override
    public File getDir(String name, int mode) 

Source Link

Usage

From source file:Main.java

public static File getImagesDirectory(Context context) {
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("images", Context.MODE_PRIVATE);
    return directory;
}

From source file:Main.java

public static String saveToInternalStorage(Bitmap bitmap, String title, Context context) throws IOException {
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("bitmaps", Context.MODE_PRIVATE);
    File image = new File(directory, title);
    FileOutputStream fileOutputStream = null;
    try {/* ww w.j  a  v  a  2 s  . c  o m*/
        fileOutputStream = new FileOutputStream(image);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        fileOutputStream.close();
    }
    return directory.getAbsolutePath();
}

From source file:Main.java

public static Bitmap loadImageFromStorage(Context context) {
    Bitmap bitmap = null;/*from   w  w  w  .ja  v  a 2 s .c o  m*/
    try {
        ContextWrapper cw = new ContextWrapper(context);
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        File f = new File(directory, "gravatar.jpg");
        bitmap = BitmapFactory.decodeStream(new FileInputStream(f));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return bitmap;

}

From source file:Main.java

public static String saveToInternalSorage(Bitmap bitmapImage, Context context, int ID, String directoryName) {
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir(directoryName, Context.MODE_PRIVATE); // path to /data/data/yourapp/app_data/imageDir
    File path = new File(directory, "mpp_profile_pic_" + Integer.toString(ID)); // Create imageDir

    FileOutputStream fos = null;//  w ww. jav a  2  s  . co m

    try {
        fos = new FileOutputStream(path);
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos); // Use the compress method on the BitMap object to write image to the OutputStream
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return directory.getAbsolutePath();
}

From source file:Main.java

protected static void cleanUpFileDirectory() {
    if (c == null) // no application context exists
        return;//from   ww w .  j  a  v  a  2  s  .com

    ContextWrapper cw = new ContextWrapper(c.getApplicationContext());
    File directory = cw.getDir(IMAGE_DIR, Context.MODE_PRIVATE);

    File file[] = directory.listFiles();
    for (int i = 0; i < file.length; i++) {
        if (file[i].lastModified() + MAXFILEAGE < System.currentTimeMillis()) {
            file[i].delete();
        }
    }
}

From source file:Main.java

protected static File createImageFile(Context context, String fileName) throws IOException {

    ContextWrapper cw = new ContextWrapper(context);
    // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir(IMAGE_DIR, Context.MODE_PRIVATE);

    // Create imageDir
    File path = new File(directory, fileName + IMAGE_EXT);
    path.deleteOnExit();/*  w  ww . j  a v  a2s  .  c  om*/

    return path;
}

From source file:Main.java

public static String saveToInternalStorage(Context context, Bitmap bitmapImage) {
    ContextWrapper cw = new ContextWrapper(context);
    // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath = new File(directory, "gravatar.jpg");

    FileOutputStream fos = null;/*from  w w w. j a  va 2 s  .c o  m*/
    try {
        fos = new FileOutputStream(mypath);
        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return directory.getAbsolutePath();
}

From source file:Main.java

/**
 * Method to save save user image in internal storage for the device
 *
 * @param bitmapImage the downloaded bitmap
 * @param context/*from  www  .  ja va  2  s .c om*/
 * @param name        the image name (the last segment)
 * @return the saved path for th image
 */
public static String saveImageIntoInternalStorage(Bitmap bitmapImage, Context context, String name) {

    // path to /data/data/adas/app_data/imageDir
    ContextWrapper contextWrapper = new ContextWrapper(context);

    //Folder name in device android/data/
    String folderName = "UserImages";
    File directory = contextWrapper.getDir(folderName, Context.MODE_PRIVATE);

    //Create imageDir
    File imagePath = new File(directory, name);

    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(imagePath);
        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.JPEG, 50, fileOutputStream);
        fileOutputStream.close();
    } catch (Exception e) {
    }
    return directory.getAbsolutePath();
}

From source file:com.breadwallet.presenter.fragments.MainFragmentQR.java

public static File getQRImageFile(Activity app) {
    ContextWrapper cw = new ContextWrapper(app);
    // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("qrcodes", Context.MODE_PRIVATE);
    return new File(directory, "qrImage.jpeg");

}

From source file:com.example.mediastock.util.Utilities.java

/**
 * Method to delete all the files within the directory typeDir, where type can be music, image or video
 *
 * @param type    the directory type: music, video or image
 * @param context the context// w  w  w  .j a  v a 2  s .co m
 */
public static void deleteAllMediaFromInternalStorage(String type, Context context) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
    File dir = cw.getDir(type + "Dir", Context.MODE_PRIVATE);

    final File[] fileNames = dir.listFiles();

    if (fileNames.length > 0) {
        for (File file : fileNames)
            file.delete();
    }
}