【发布时间】:2019-05-25 06:34:33
【问题描述】:
我在下面有一个代码。
final URL url1 = stringToURL("linki_of_image/image1.jpg");
final URL url2 = stringToURL("linki_of_image/image2.jpg");
final URL url3 = stringToURL("linki_of_image/image3.jpg");
final URL url4 = stringToURL("linki_of_image/image4.jpg");
final URL url5 = stringToURL("linki_of_image/image5.jpg");
mMyTask = new DownloadTask().execute(url1, url2, url3, url4,url5);
private class DownloadTask extends AsyncTask<URL, Integer, List<Bitmap>> {
protected void onPreExecute() {
mProgressDialog.show();
mProgressDialog.setProgress(0);
}
protected List<Bitmap> doInBackground(URL... urls) {
int count = urls.length;
HttpURLConnection connection = null;
List<Bitmap> bitmaps = new ArrayList<>();
for (int i = 0; i < count; i++) {
URL currentURL = urls[i];
try {
connection = (HttpURLConnection) currentURL.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);
bitmaps.add(bmp);
publishProgress((int) (((i + 1) / (float) count) * 100));
if (isCancelled()) {
break;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
connection.disconnect();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return bitmaps;
}
protected void onProgressUpdate(Integer... progress) {
mProgressDialog.setProgress(progress[0]);
}
protected void onCancelled() {
Toast.makeText(mContext, "Task cancelled", Toast.LENGTH_SHORT).show();
}
protected void onPostExecute(List<Bitmap> result) {
mProgressDialog.dismiss();
mLLayout.removeAllViews();
for (int i = 0; i < result.size(); i++) {
Bitmap bitmap = result.get(i);
Uri imageInternalUri = saveImageToInternalStorage(bitmap, i);
addNewImageViewToLayout(bitmap);
addNewImageViewToLayout(imageInternalUri);
}
}
}
protected URL stringToURL(String urlString) {
try {
URL url = new URL(urlString);
return url;
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
protected Uri saveImageToInternalStorage(Bitmap bitmap, int index) {
File file = sdCardDirectory;
file = new File(file, "UniqueFileName" + index + ".jpg");
try {
OutputStream stream = null;
stream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
Uri savedImageURI = Uri.parse(file.getAbsolutePath());
return savedImageURI;
}
protected void addNewImageViewToLayout(Bitmap bitmap) {
ImageView iv = new ImageView(getApplicationContext());
iv.setImageBitmap(bitmap);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 500);
iv.setLayoutParams(lp);
mLLayout.addView(iv);
}
protected void addNewImageViewToLayout(Uri uri) {
ImageView iv = new ImageView(getApplicationContext());
iv.setImageURI(uri);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 300);
iv.setLayoutParams(lp);
mLLayout.addView(iv);
}
上面的代码从服务器下载图像,它完美地工作我这里唯一的问题是当它下载图像时代码重命名它。
我的问题是如何下载或创建仍然具有相同文件名的图像(因为我注意到正在创建图像)
因为saveImageToInternalStorage 部分将其重命名为新文件
【问题讨论】:
-
那你为什么不能改变呢?有什么问题?
-
其实我不知道从哪里开始,因为这是我第一次做。我只需要下载文件而不重命名它
标签: android file android-asynctask