本文整理汇总了Java中android.app.Activity.getCacheDir方法的典型用法代码示例。如果您正苦于以下问题:Java Activity.getCacheDir方法的具体用法?Java Activity.getCacheDir怎么用?Java Activity.getCacheDir使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.app.Activity
的用法示例。
在下文中一共展示了Activity.getCacheDir方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadFont
import android.app.Activity; //导入方法依赖的package包/类
@ReactMethod
public void loadFont(final ReadableMap options, final Callback callback) throws Exception {
Activity currentActivity = getCurrentActivity();
if (currentActivity == null) {
callback.invoke("Invalid activity");
return;
}
String name = (options.hasKey("name")) ? options.getString("name") : null,
data = (options.hasKey("data")) ? options.getString("data") : null,
type = null;
if (name == null || name.length() == 0) {
callback.invoke("Name property empty");
return;
}
if (data == null || data.length() == 0) {
callback.invoke("Data property empty");
return;
}
if (("data:").equalsIgnoreCase(data.substring(0, 5))) {
Integer pos = data.indexOf(',');
if (pos > 0) {
String[] encodingParams = data.substring(5, pos).split(";");
String mimeType = encodingParams[0];
data = data.substring(pos + 1);
if (("application/x-font-ttf").equalsIgnoreCase(mimeType) ||
("application/x-font-truetype").equalsIgnoreCase(mimeType) ||
("font/ttf").equalsIgnoreCase(mimeType)) {
type = "ttf";
} else if (("application/x-font-opentype").equalsIgnoreCase(mimeType) ||
("font/opentype").equalsIgnoreCase(mimeType)) {
type = "otf";
}
}
}
if (options.hasKey("type"))
type = options.getString("type");
if (type == null)
type = "ttf";
try {
byte[] decodedBytes = Base64.decode(data, Base64.DEFAULT);
File cacheFile = new File(currentActivity.getCacheDir(), "tempFont" + (tempNameCounter++) + type);
FileOutputStream stream = new FileOutputStream(cacheFile);
try {
stream.write(decodedBytes);
} finally {
stream.close();
}
//Load the font from the temporary file we just created
Typeface typeface = Typeface.createFromFile(cacheFile);
//Cache the font for react
ReactFontManager.getInstance().setTypeface(name, typeface.getStyle(), typeface);
cacheFile.delete();
} catch(Exception e) {
callback.invoke(e.getMessage());
} finally {
callback.invoke(null, name);
}
}
示例2: getTypeDir
import android.app.Activity; //导入方法依赖的package包/类
/**
* Get the base directory for storing the given file type.
*
* @param activity Activity
* @param type File type
* @return Base directory as File
*/
private static File getTypeDir(Activity activity, byte type) {
return type == TYPE_CACHE ? activity.getCacheDir() : activity.getFilesDir();
}