Example usage for android.content ContextWrapper ContextWrapper

List of usage examples for android.content ContextWrapper ContextWrapper

Introduction

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

Prototype

public ContextWrapper(Context base) 

Source Link

Usage

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;/*  ww  w  .  jav  a2s  . c om*/

    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

public static void setupDatabase(Context context, String db_name) {
    ContextWrapper cw = new ContextWrapper(context);
    String db_path = cw.getDatabasePath(db_name).getPath();

    try {//ww  w.j a va 2s .c  o m
        // Setup
        byte[] buffer = new byte[1024];
        int length;
        InputStream myInput = context.getAssets().open(db_name);
        OutputStream myOutput = new FileOutputStream(db_path);

        // Write all the things.
        while ((length = myInput.read(buffer)) > 0)
            myOutput.write(buffer, 0, length);

        // Cleanup
        myOutput.close();
        myOutput.flush();
        myInput.close();
    } catch (IOException e) {
        // You done goofed.
        e.printStackTrace();
    }
}

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 www  . j av  a2  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

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 {/*w  w  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

private static void bindToService(Intent intent, Context context, ServiceConnection callback) {
    Context realActivity = context.getApplicationContext();
    if (realActivity == null) {
        realActivity = context;/*from ww  w.j ava 2  s.  c  o  m*/
    }
    ContextWrapper cw = new ContextWrapper(realActivity);
    cw.startService(intent);
    cw.bindService(intent, callback, Context.BIND_AUTO_CREATE);
}

From source file:Main.java

public static Bitmap loadImageFromStorage(Context context) {
    Bitmap bitmap = null;/* w ww .  j ava  2  s.  com*/
    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 File getDataDirectory(Context ctx) {
    File f;/*from w ww .j  a v  a2 s.  c o  m*/

    if (Environment.getExternalStorageState().equals("mounted")) {
        //Has SD Card mounted
        f = Environment.getExternalStorageDirectory();
    } else {
        ContextWrapper c = new ContextWrapper(ctx);

        f = c.getFilesDir();
    }

    return f;
}

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

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();/*from ww  w. j  a  v  a2s. c om*/

    return path;
}

From source file:Main.java

protected static void cleanUpFileDirectory() {
    if (c == null) // no application context exists
        return;//  w w  w.  j av a  2  s  . co m

    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();
        }
    }
}