【发布时间】:2017-08-10 05:56:03
【问题描述】:
朋友们好! 我只是想将图像从 url 下载到内部存储 http://github.com/google/fonts/blob/master/apache/roboto/Roboto-Regular.ttf?raw=true。我已经创建了目录。
try
{
File folder = new File(getFilesDir() + "/"+"SS");
if (!folder.exists())
{
folder.mkdir();
Log.i("Directory","Created");
}
//URL connection
URL url = new URL(fonturl);
HttpURLConnection c = (HttpURLConnection)
url.openConnection();
c.connect();
File apkStorage=new File(getFilesDir().getPath());
Log.i("ApkStorage",""+apkStorage);
outputFile=new File(apkStorage.getAbsolutePath() +"/");
if(!outputFile.exists())
{
try {
outputFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Log.e("FIle", "File Created");
}
FileOutputStream fos = new FileOutputStream(outputFile);
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());
}
}
它只是创建我指定的文件名 download.jpg 不将文件从 url 下载到内部存储。
错误:未在我的目录中下载字体显示类似这样的错误 Download Error Exception /data/user/0/sdk.nfnlabs.in.customfonts/files(是一个目录)。它应该直接下载到我的目录,如roboto.tff,而不提供文件名。
【问题讨论】:
-
您可以与问题分享什么错误
-
请分享错误日志
-
请帮我解决我的错误
-
可能
outputFile=new File(apkStorage.getAbsolutePath() +"/");是错误.. 它应该是outputFile=new File(apkStorage.getAbsolutePath() +"/filename.ttf"); -
是的,我也试过了。如果我把 robots.ttf 它只是创建文件但我需要文件必须下载