下面列出了android.content.ContextWrapper#getDir ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
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;
}
}
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;
}
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();
}
}
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;
}
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;
}
}
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;
}
}
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;
}
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);
}
}
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;
}
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();
}
}
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");
}
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;
}
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();
}
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;
}
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);
}
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();
}