【发布时间】:2016-04-14 12:37:07
【问题描述】:
我的代码工作正常,它将图像下载到 sd 卡,但是,我在定义我的 sd 卡路径时收到此警告“不要硬编码”/sdcard/”;使用 Environment.getExternalStorageDirectory().getPath( ) 而不是"
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/.temp");//.temp is the image file name
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC", progress[0]);
}
问题是,如果我使用建议的解决方案,那么我将无法给我下载的文件一个新名称(“.temp”)
【问题讨论】:
-
OutputStream output = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), ".temp").getAbsolutePath())
标签: android