本文整理匯總了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();
}