【问题标题】:Download Image with the same file name下载同名图片
【发布时间】: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


【解决方案1】:

基本上服务器也会给你文件的名称,你只需要从标题中获取它并将其应用于保存它而不是使用你唯一的名称逻辑

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

这个答案会对你有所帮助

【讨论】:

    【解决方案2】:
    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);
    

    【讨论】:

    • 好的,先生,我明白了。下载图片需要重命名。我想下载它与该图像的文件名相同
    • 先生,我在哪里可以在我发布的代码上添加这些代码?
    • 上面的代码完全按照您的意愿行事,它从链接中获取文件名并使用该名称和扩展名保存文件。你想要什么是不可能的,因为你正在使用“new File()”创建文件,你必须自己提供一个名称
    • 在 saveImageToInternalStorage 方法之后,您还必须将下载链接应用到该方法才能使用 getName 方法 String baseNameWithExtension = FilenameUtils.getName("linki_of_image/image1.jpg");文件文件 = sdCardDirectory; file = new File(file, baseNameWithExtension);
    • 哇..我在这个方面很糟糕..我没有看到它会创建一个新文件,所以需要一个名称我只想创建一个具有相同名称的新文件
    猜你喜欢
    • 2020-02-15
    • 2018-03-07
    • 2022-12-07
    • 1970-01-01
    • 2019-01-31
    • 2016-02-25
    • 2010-11-23
    • 2012-12-31
    • 2013-08-18
    相关资源
    最近更新 更多