android.content.ContextWrapper#getDir ( )源码实例Demo

下面列出了android.content.ContextWrapper#getDir ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: kcanotify_h5-master   文件: KcaUtils.java
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;
        }
    }
 
源代码2 项目: kcanotify_h5-master   文件: KcaUtils.java
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;
    }
}
 
源代码3 项目: kcanotify_h5-master   文件: KcaUtils.java
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;
}
 
源代码4 项目: Quran-For-My-Android   文件: MainActivity.java
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();
	}
}
 
源代码5 项目: kcanotify_h5-master   文件: KcaUtils.java
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;
}
 
源代码6 项目: kcanotify   文件: KcaUtils.java
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;
    }
}
 
源代码7 项目: kcanotify   文件: KcaUtils.java
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;
        }
    }
 
源代码8 项目: kcanotify   文件: KcaUtils.java
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;
}
 
源代码9 项目: kcanotify   文件: KcaUtils.java
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);
    }
}
 
源代码10 项目: kcanotify   文件: KcaUtils.java
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;
}
 
源代码11 项目: Aerlink-for-Android   文件: UtilsServiceHandler.java
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();
    }
}
 
源代码12 项目: kcanotify_h5-master   文件: KcaUtils.java
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;
}
 
@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");
}
 
源代码14 项目: kcanotify   文件: KcaUtils.java
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;
}
 
源代码15 项目: kcanotify   文件: KcaUtils.java
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;
}
 
@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();
}
 
源代码17 项目: smartcoins-wallet   文件: Helper.java
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;

}
 
源代码18 项目: kcanotify_h5-master   文件: KcaUtils.java
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();
}
 
public OtherActivityHandler(Context context) {
    this.context = context;
    ContextWrapper cw = new ContextWrapper(context);
    ImageDirectory = cw.getDir("imageDir", Context.MODE_PRIVATE);
}
 
源代码20 项目: kcanotify   文件: KcaUtils.java
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();
}