Java 下载图像会覆盖上一个图像
您好,我正在使用这个功能从URL下载图像,这是工作正常。但它会覆盖以前下载的图像Java 下载图像会覆盖上一个图像,java,android,image,download,Java,Android,Image,Download,您好,我正在使用这个功能从URL下载图像,这是工作正常。但它会覆盖以前下载的图像 void downloadFile(){ try { Bundle bundle = getIntent().getExtras(); String[] imageUrls = bundle.getStringArray(Extra.IMAGES); // Log.v(imageUrls, "Creating view..."); in
void downloadFile(){
try {
Bundle bundle = getIntent().getExtras();
String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
// Log.v(imageUrls, "Creating view...");
int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);
URL url = new URL(imageUrls[pagerPosition]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//connect
urlConnection.connect();
//set the path where we want to save the file
File SDCardRoot = Environment.getExternalStorageDirectory();
//create a new file, to save the downloaded file
File file = new File(SDCardRoot,"image.png");
FileOutputStream fileOutput = new FileOutputStream(file);
//Stream used for reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file which we are downloading
totalSize = urlConnection.getContentLength();
runOnUiThread(new Runnable() {
public void run() {
pb.setMax(totalSize);
}
});
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
// update the progressbar //
runOnUiThread(new Runnable() {
public void run() {
pb.setProgress(downloadedSize);
float per = ((float)downloadedSize/totalSize) * 100;
cur_val.setText("Downloaded " + downloadedSize + "KB / " + totalSize + "KB (" + (int)per + "%)" );
}
});
}
//close the output stream when complete //
fileOutput.close();
runOnUiThread(new Runnable() {
public void run() {
// pb.dismiss(); // if you want close it..
}
});
} catch (final MalformedURLException e) {
showError("Error : MalformedURLException " + e);
e.printStackTrace();
} catch (final IOException e) {
showError("Error : IOException " + e);
e.printStackTrace();
}
catch (final Exception e) {
showError("Error : Please check your internet connection " + e);
}
}
我怎样才能在上面一行中的每次新下载中更改名称。。它不接受任何不是字符串的东西
任何帮助都会很好 这是因为您总是使用相同的名称保存图像image.png,因此请使用当前日期时间更改名称 所以改变这个
File file = new File(SDCardRoot,"image.png");
到
编辑您也可以使用from
日历
作为
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
Date now = new Date();
String fileName = "image" + formatter.format(now) + ".png";
File file = new File(SDCardRoot,fileName);
你可以这样做:
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
String fileName = "image" + formatter.format(calendar.getTime()) + ".png";
File file = new File(SDCardRoot,fileName);
@用户3652550很乐意帮忙。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受,以帮助他人also@user3652550是的,5分钟后您将能够接受,但请享受编码。@user3652550您也可以使用日历,请参阅答案的编辑部分。
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
String fileName = "image" + formatter.format(calendar.getTime()) + ".png";
File file = new File(SDCardRoot,fileName);
Calender calendar=Calender.getInstance();
String fileName = "image_" + calendar.getTimeInMillis() + ".png";
File file = new File(SDCardRoot,fileName);