フォントサイズが大きすぎてassets フォルダで未圧縮の状態では使えない場合。
assets フォルダに圧縮したフォントファイルをおいて、アプリケーション領域にコピーして使う。
アプリの容量は、4GB にまで拡大されたが、APK ファイル自体のサイズは50MBまで、
拡張ファイル1つあたりの制限は2GB までなので2GBを超えるフォントサイズでは使えないので、WEBからダウンロードする方法を採らないといけないかも。
assetsフォルダの圧縮ファイルを解凍してアプリケーション領域のfilesフォルダにコピーする。
一つのファイルが圧縮されている場合。
try {
AssetManager am = getResources().getAssets();
InputStream is = am.open("ipaexm.zip", AssetManager.ACCESS_STREAMING);
ZipInputStream zis = new ZipInputStream(is);
ZipEntry ze = zis.getNextEntry();
if (ze != null) {
path = getFilesDir().toString() + "/" + ze.getName();
FileOutputStream fos = new FileOutputStream(path, false);
byte[] buf = new byte[1024];
int size = 0;
while ((size = zis.read(buf, 0, buf.length)) > -1) {
fos.write(buf, 0, size);
}
fos.close();
zis.closeEntry();
}
zis.close();
} catch (Exception e) {
e.printStackTrace();
}
圧縮ファイルに複数のファイルが含まれているとき
try {
AssetManager am = getResources().getAssets();
InputStream is = am.open("ipaexm.zip", AssetManager.ACCESS_STREAMING);
ZipInputStream zis = new ZipInputStream(is);
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
path = getFilesDir().toString() + "/" + ze.getName();
FileOutputStream fos = new FileOutputStream(path, false);
byte[] buf = new byte[1024];
int size = 0;
while ((size = zis.read(buf, 0, buf.length)) > -1) {
fos.write(buf, 0, size);
}
fos.close();
zis.closeEntry();
ze = zis.getNextEntry();
}
zis.close();
} catch (Exception e) {
e.printStackTrace();
}
フォントファイルがアプリケーションデータ領域のfilesフォルダに保存されている場合
TextView tv2 = (TextView)findViewById(R.id.textView2);
TextView tv3 = (TextView)findViewById(R.id.textView3);
Typeface typeface = Typeface.createFromFile("/data/data/com.ymato.font/files/ipaexg.ttf");
Typeface typeface2 = Typeface.createFromFile("/data/data/com.ymato.font/files/ipaexm.ttf");
tv2.setTypeface(typeface);//
tv3.setTypeface(typeface2);//
フォントファイルがSDカードに入っている場合
Typeface typeface = Typeface.createFromFile( new File(Environment.getExternalStorageDirectory(), "ipaexm.ttf"));
assetsフォルダには1MB以上の非圧縮ファイルを設置できないので、
大きいサイズのフォントファイルを置いたらエラーが出た
Data exceeds UNCOMPRESS_DATA_MAX (6022748 vs 1048576)
FileOutputStream fos = new FileOutputStream(path, false); ではパーミッションが
-rw------- になるので変更する場合
FileOutputStream fos = openFileOutput( ze.getName() , MODE_WORLD_READABLE);
ファイルの指定方法が違う、上は/data/data/[app name]/files/fileName,
下は、ファイル名のみ、
MODE_PRIVATE このアプリからのみ使用
MODE_APPEND 現在のファイルに追記
MODE_WORLD_READABLE 他のアプリからも読み込み可能
MODE_WORLD_WRITEABLE 他のアプリからも書き込み可能

0 件のコメント:
コメントを投稿