String path = getExternalCacheDir() + "/myfont.ttf";
//let us copy the font on private storage.
copyFiletoExternalStorage(R.raw.myfont2, "myfont.ttf");
//let us load the font
Typeface myTypeface = Typeface.createFromFile(path);
//let us set the font to text view
txt2.setTypeface(myTypeface);
}
private void copyFiletoExternalStorage(int resourceId, String resourceName){
String pathSDCard = getExternalCacheDir() + "/myfont.ttf";//Environment.getExternalStorageDirectory() + "/Android/data/" + resourceName;
try{
java.io.InputStream in = getResources().openRawResource(resourceId);
java.io.FileOutputStream out = null;
out = new java.io.FileOutputStream(pathSDCard);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
} catch (java.io.FileNotFoundException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
}
Komentar