Java Code Examples for android.content.ContextWrapper#getDir()

The following examples show how to use android.content.ContextWrapper#getDir() . These examples are extracted from open source projects. 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
public static JsonArray getJsonArrayFromStorage(Context context, String name, KcaDBHelper helper) {

        if (getBooleanPreferences(context, PREF_RES_USELOCAL)) {
            return getJsonArrayFromAsset(context, name, helper);
        } else {
            ContextWrapper cw = new ContextWrapper(context);
            File directory = cw.getDir("data", Context.MODE_PRIVATE);
            File jsonFile = new File(directory, name);
            JsonArray data = new JsonArray();
            try {
                Reader reader = new FileReader(jsonFile);
                data = new JsonParser().parse(reader).getAsJsonArray();
                reader.close();
            } catch (IOException | IllegalStateException | JsonSyntaxException e ) {
                e.printStackTrace();
                setPreferences(context, PREF_DATALOAD_ERROR_FLAG, true);
                if (helper != null) helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getJsonArrayFromStorage", "0", getStringFromException(e));
                data = getJsonArrayFromAsset(context, name, helper);
            }
            return data;
        }
    }
 
Example 2
public static JsonObject getJsonObjectFromStorage(Context context, String name, KcaDBHelper helper) {
    if (getBooleanPreferences(context, PREF_RES_USELOCAL)) {
        return getJsonObjectFromAsset(context, name, helper);
    } else {
        ContextWrapper cw = new ContextWrapper(context);
        File directory = cw.getDir("data", Context.MODE_PRIVATE);
        File jsonFile = new File(directory, name);
        JsonObject data = null;
        try {
            Reader reader = new FileReader(jsonFile);
            data = new JsonParser().parse(reader).getAsJsonObject();
            reader.close();
        } catch (IOException | IllegalStateException | JsonSyntaxException e) {
            e.printStackTrace();
            setPreferences(context, PREF_DATALOAD_ERROR_FLAG, true);
            if (helper != null) helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getJsonObjectFromStorage", "0", getStringFromException(e));
            data = getJsonObjectFromAsset(context, name, helper);
        }
        return data;
    }
}
 
Example 3
public static boolean checkFairyImageFileFromStorage(Context context, String name) {
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("fairy", Context.MODE_PRIVATE);
    File myImageFile = new File(directory, KcaUtils.format("%s", name));
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    try {
        InputStream is = new FileInputStream(myImageFile);
        bitmap = BitmapFactory.decodeStream(is, null, options);
        is.close();
    } catch (IOException e) {
        // Log.e("KCA", getStringFromException(e));
        return false;
    }
    if (bitmap == null) {
        return false;
    }
    return true;
}
 
Example 4
private void prepareAudioStorageDir(){
	String state=Environment.getExternalStorageState();
	// has writable external  storage
	if (Environment.MEDIA_MOUNTED.equals(state)) {
		audioStorageDir = new File(Environment.getExternalStorageDirectory(),storageFolderName+"/"+AudioStorageDirName);
	} else {
		ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
		audioStorageDir = contextWrapper.getDir(storageFolderName+"/"+AudioStorageDirName,
				Context.MODE_PRIVATE);
	}
	
	if(!audioStorageDir.exists()){
		audioStorageDir.mkdirs();
	}else{//check if new 10 char file added, if yes then shorten
		new AudioFileFolderizingTask(this).execute();
	}
}
 
Example 5
public static boolean validateResourceFiles(Context context, KcaDBHelper helper) {
    int count = 0;
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("data", Context.MODE_PRIVATE);
    for (final File entry : directory.listFiles()) {
        try {
            Reader reader = new FileReader(entry);
            new JsonParser().parse(reader);
            count += 1;
        } catch (FileNotFoundException | IllegalStateException | JsonSyntaxException e ) {
            e.printStackTrace();
            if (helper != null) helper.recordErrorLog(ERROR_TYPE_DATALOAD, entry.getName(), "validateResourceFiles", "2", getStringFromException(e));
            setPreferences(context, PREF_DATALOAD_ERROR_FLAG, true);
            return false;
        }
    }
    return count > 0;
}
 
Example 6
Source Project: kcanotify   File: KcaUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static JsonObject getJsonObjectFromStorage(Context context, String name, KcaDBHelper helper) {
    if (getBooleanPreferences(context, PREF_RES_USELOCAL)) {
        return getJsonObjectFromAsset(context, name, helper);
    } else {
        ContextWrapper cw = new ContextWrapper(context);
        File directory = cw.getDir("data", Context.MODE_PRIVATE);
        File jsonFile = new File(directory, name);
        JsonObject data = null;
        try {
            Reader reader = new FileReader(jsonFile);
            data = new JsonParser().parse(reader).getAsJsonObject();
            reader.close();
        } catch (IOException | IllegalStateException | JsonSyntaxException e) {
            e.printStackTrace();
            setPreferences(context, PREF_DATALOAD_ERROR_FLAG, true);
            if (helper != null) helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getJsonObjectFromStorage", "0", getStringFromException(e));
            data = getJsonObjectFromAsset(context, name, helper);
        }
        return data;
    }
}
 
Example 7
Source Project: kcanotify   File: KcaUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static JsonArray getJsonArrayFromStorage(Context context, String name, KcaDBHelper helper) {

        if (getBooleanPreferences(context, PREF_RES_USELOCAL)) {
            return getJsonArrayFromAsset(context, name, helper);
        } else {
            ContextWrapper cw = new ContextWrapper(context);
            File directory = cw.getDir("data", Context.MODE_PRIVATE);
            File jsonFile = new File(directory, name);
            JsonArray data = new JsonArray();
            try {
                Reader reader = new FileReader(jsonFile);
                data = new JsonParser().parse(reader).getAsJsonArray();
                reader.close();
            } catch (IOException | IllegalStateException | JsonSyntaxException e ) {
                e.printStackTrace();
                setPreferences(context, PREF_DATALOAD_ERROR_FLAG, true);
                if (helper != null) helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getJsonArrayFromStorage", "0", getStringFromException(e));
                data = getJsonArrayFromAsset(context, name, helper);
            }
            return data;
        }
    }
 
Example 8
Source Project: kcanotify   File: KcaUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static boolean checkFairyImageFileFromStorage(Context context, String name) {
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("fairy", Context.MODE_PRIVATE);
    File myImageFile = new File(directory, KcaUtils.format("%s", name));
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    try {
        InputStream is = new FileInputStream(myImageFile);
        bitmap = BitmapFactory.decodeStream(is, null, options);
        is.close();
    } catch (IOException e) {
        // Log.e("KCA", getStringFromException(e));
        return false;
    }
    if (bitmap == null) {
        return false;
    }
    return true;
}
 
Example 9
Source Project: kcanotify   File: KcaUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static void setFairyImageFromStorage(Context context, String name, ImageView view, int dp) {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    int fairy_id = Integer.parseInt(name.replace("noti_icon_", ""));
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("fairy", Context.MODE_PRIVATE);
    File myImageFile = new File(directory, KcaUtils.format("%s.png", name));
    if (myImageFile.exists()) {
        if (px > 0) {
            GlideApp.with(context).load(myImageFile.getPath())
                    .dontAnimate().override(px, px).into(view);
        } else {
            GlideApp.with(context).load(myImageFile.getPath()).into(view);
        }
    } else if (FAIRY_SPECIAL_FLAG && fairy_id >= FAIRY_SPECIAL_PREFIX) {
        view.setImageResource(getId(name, R.mipmap.class));
    } else {
        view.setImageResource(R.mipmap.noti_icon_0);
    }
}
 
Example 10
Source Project: kcanotify   File: KcaUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static boolean validateResourceFiles(Context context, KcaDBHelper helper) {
    int count = 0;
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("data", Context.MODE_PRIVATE);
    for (final File entry : directory.listFiles()) {
        try {
            Reader reader = new FileReader(entry);
            new JsonParser().parse(reader);
            count += 1;
        } catch (FileNotFoundException | IllegalStateException | JsonSyntaxException e ) {
            e.printStackTrace();
            if (helper != null) helper.recordErrorLog(ERROR_TYPE_DATALOAD, entry.getName(), "validateResourceFiles", "2", getStringFromException(e));
            setPreferences(context, PREF_DATALOAD_ERROR_FLAG, true);
            return false;
        }
    }
    return count > 0;
}
 
Example 11
private void saveToInternalStorage(Bitmap bitmap, String bundleIdentifier) {
    Log.i(LOG_TAG, "Saving icon");

    ContextWrapper cw = new ContextWrapper(mContext);
    File directory = cw.getDir("AppIconDir", Context.MODE_PRIVATE);
    File path = new File(directory, bundleIdentifier+".png");

    try (FileOutputStream fos = new FileOutputStream(path)) {
        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        Log.i(LOG_TAG, "Icon saved");
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 12
public static Bitmap getFairyImageFromStorage(Context context, String name, KcaDBHelper helper) {
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("fairy", Context.MODE_PRIVATE);
    File myImageFile = new File(directory, KcaUtils.format("%s.png", name));

    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    int fairy_id = Integer.parseInt(name.replace("noti_icon_", ""));
    if (fairy_id == 0) {
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.noti_icon_0);
    } else if (FAIRY_SPECIAL_FLAG && fairy_id >= FAIRY_SPECIAL_PREFIX) {
        bitmap = BitmapFactory.decodeResource(context.getResources(), getId(name, R.mipmap.class));
    } else {
        try {
            bitmap = BitmapFactory.decodeStream(new FileInputStream(myImageFile), null, options);
        } catch (FileNotFoundException e) {
            try {
                int item_id =KcaUtils.getId(name, R.mipmap.class);
                bitmap = BitmapFactory.decodeResource(KcaApplication.getInstance().getResources(), item_id);
            } catch (Exception ex){
                e.printStackTrace();
            }
            if(bitmap == null) {
                setPreferences(context, PREF_DATALOAD_ERROR_FLAG, true);
                if (helper != null)
                    helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getFairyImageFromStorage", "0", getStringFromException(e));
                bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.noti_icon_0);
            }
        }
        if (bitmap == null) {
            setPreferences(context, PREF_DATALOAD_ERROR_FLAG, true);
            if (helper != null) helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getFairyImageFromStorage", "0", "bitmap==null");
            bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.noti_icon_0);
        }
    }
    return bitmap;
}
 
Example 13
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    ContextWrapper cw = new ContextWrapper(getActivity().getApplicationContext());
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    mFile = new File(directory, System.currentTimeMillis() + ".png");
}
 
Example 14
Source Project: kcanotify   File: KcaUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static boolean clearFairyImageFileFromStorage(Context context) {
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("fairy", Context.MODE_PRIVATE);
    for (File file: directory.listFiles()) {
        if (!file.isDirectory())
            file.delete();
    }
    return true;
}
 
Example 15
Source Project: kcanotify   File: KcaUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static Bitmap getFairyImageFromStorage(Context context, String name, KcaDBHelper helper) {
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("fairy", Context.MODE_PRIVATE);
    File myImageFile = new File(directory, KcaUtils.format("%s.png", name));

    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    int fairy_id = Integer.parseInt(name.replace("noti_icon_", ""));
    if (fairy_id == 0) {
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.noti_icon_0);
    } else if (FAIRY_SPECIAL_FLAG && fairy_id >= FAIRY_SPECIAL_PREFIX) {
        bitmap = BitmapFactory.decodeResource(context.getResources(), getId(name, R.mipmap.class));
    } else {
        try {
            bitmap = BitmapFactory.decodeStream(new FileInputStream(myImageFile), null, options);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            setPreferences(context, PREF_DATALOAD_ERROR_FLAG, true);
            if (helper != null) helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getFairyImageFromStorage", "0", getStringFromException(e));
            bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.noti_icon_0);
        }
        if (bitmap == null) {
            setPreferences(context, PREF_DATALOAD_ERROR_FLAG, true);
            if (helper != null) helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getFairyImageFromStorage", "0", "bitmap==null");
            bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.noti_icon_0);
        }
    }
    return bitmap;
}
 
Example 16
@Override
public String getProductImagePath(String id)
{
    ContextWrapper contextWrapper = new ContextWrapper(context);
    File directory = contextWrapper.getDir(IMAGE_DIR_NAME, Context.MODE_PRIVATE);
    File path = new File(directory, getUniqueName(id));
    return path.getAbsolutePath();
}
 
Example 17
Source Project: smartcoins-wallet   File: Helper.java    License: MIT License 5 votes vote down vote up
public static Bitmap loadImageFromStorage(Context context) {
    Bitmap bitmap = null;
    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;

}
 
Example 18
public static boolean checkFairyImageInStorage(Context context, String name) {
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("fairy", Context.MODE_PRIVATE);
    File image = new File(directory, name);
    return image.exists();
}
 
Example 19
public OtherActivityHandler(Context context) {
    this.context = context;
    ContextWrapper cw = new ContextWrapper(context);
    ImageDirectory = cw.getDir("imageDir", Context.MODE_PRIVATE);
}
 
Example 20
Source Project: kcanotify   File: KcaUtils.java    License: GNU General Public License v3.0 4 votes vote down vote up
public static boolean checkFairyImageInStorage(Context context, String name) {
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("fairy", Context.MODE_PRIVATE);
    File image = new File(directory, name);
    return image.exists();
}