喜的朋友有很多困惑。 第一件事情就是它不是在存儲設備上,而不是它存儲在我的模擬器這條路徑的數據/數據/ com.customfonts /文件/ Robotoo.ttf下。然後,當我試圖讓沒有找到,因爲它在數據/用戶/ 0/com.customfonts/Robotoo.ttf搜索,而不是搜索data.data/com.customfonts/files/Robotoo.ttf文件時拋出運行時異常文件。內部存儲不是存儲文件
getDirectory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
new DownloadingTask().execute();
Log.i("FilePAthFirst",""+getFilesDir());
}
});
btnGETDATA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String filename="Robotoo.ttf";
getTypeface(filename);
}
});
private Typeface getTypeface(String filename)
{
Typeface font;
try
{
font = Typeface.createFromFile(getFilesDir() +"/"+filename);
Log.i("FOnt found",""+font);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
return font;
}
private class DownloadingTask extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL(fonturl);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.connect();
FileOutputStream fos = new FileOutputStream(getApplicationContext().getFilesDir()+ "Robotoo.ttf");
Log.i("Download","complete");
Log.i("FOS",""+fos.toString());
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
}
catch (Exception e) {
e.printStackTrace();
outputFile = null;
Log.e("Error", "Download Error Exception " + e.getMessage());
}
return null;
}
}
你要我分享什麼..? DownloadingTask ..? –
@ Ssri89我更新了我的答案。 –