下载具有相同文件名的图像

时间:2018-12-27 07:44:33

标签: android file android-asynctask

我在下面有一个代码。

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将其重命名为新文件

2 个答案:

答案 0 :(得分:0)

String baseName = FilenameUtils.getBaseName("linki_of_image/image1.jpg");
  

baseName = image1

String baseNameWithExtension = FilenameUtils.getName("linki_of_image/image1.jpg");
  

baseNameWithExtension = image1.jpg

回答您的问题;

我的问题是如何在不重命名的情况下下载这些图像? 或在链接上输入相同的新名称

File file = sdCardDirectory;
file = new File(file, baseNameWithExtension);

答案 1 :(得分:0)

基本上,服务器也会为您提供文件名,您只需从标题中获取文件并将其应用于保存文件即可,而不用使用唯一的名称逻辑

https://stackoverflow.com/a/10995501/4804264

此答案将为您提供帮助