【问题标题】:Retrofit - Download file using url in response改造 - 使用 url 下载文件作为响应
【发布时间】:2018-09-06 13:26:43
【问题描述】:

这清楚地表明媒体文件 url 在收到响应后被接收到客户端。 我的问题是如何使用在收到回复之前无法访问的 url 下载文件? 是否有可能在获取url的同时下载文件,或者接收url然后下载文件?

我被困在这里,谁能在这里简要解释一下解决方案?

感谢任何帮助!

【问题讨论】:

  • 接收网址然后下载文件 -- 这是唯一可能的选择
  • 好的!!!所以我必须打电话并得到回复,然后再次发出另一个下载请求。就是这样!

标签: android retrofit retrofit2 okhttp3 okhttp


【解决方案1】:

您需要在模型中解析 Json。

然后就可以获取url属性并下载到文件系统了。您可以使用 HttpUrlConnection 来执行此操作,或者如果您想将 Picasso 库与 Target Picasso 一起使用并将其下载到文件系统中。

让我给你举个例子。

使用毕加索

        public static void downloadImageWithPicasso(Context context, String url) {
            Picasso.with(context)
                    .load(your_url_from_model)
                    .into(saveUsingTarget(url));
        }

        private static Target saveUsingTarget(final String url){
            Target target = new Target(){

                @Override
                public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
                    new Thread(new Runnable() {

                        @Override
                        public void run() {

                            File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + url);
                            try {
                                file.createNewFile();
                                FileOutputStream ostream = new FileOutputStream(file);
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
                                ostream.flush();
                                ostream.close();
                            } catch (IOException e) {
                                Log.e("IOException", e.getLocalizedMessage());
                            }
                        }
                    }).start();

                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {

                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }
            };
            return target;
        }

使用 HttpUrlConnection

public void downloadImageWithUrlConnection(){
        try{

            URL url = new URL("your_url_from_model");

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.connect();

            File sdCardPath = Environment.getExternalStorageDirectory().getAbsoluteFile();

            String filename = "file_name.png";


            File file = new File(sdCardPath,filename);
            file.createNewFile();

            FileOutputStream fileOutput = new FileOutputStream(file);
            InputStream inputStream = urlConnection.getInputStream();

            byte[] buffer = new byte[1024];
            int bufferLength = 0;

            while ( (bufferLength = inputStream.read(buffer)) > 0 ){
                fileOutput.write(buffer, 0, bufferLength);

            }

            fileOutput.close();

        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

重要提示:如果您想使用 HttpURLConnection 选项,您需要在后台线程中运行它。

希望对你有帮助。

【讨论】:

  • 我使用Retrofit进行网络操作,我使用Glide进行图像显示,Glide会有用吗,否则我必须为毕加索更改
  • 如果你想继续使用 Glide 你有这个问题:stackoverflow.com/questions/44761720/…我希望这对你有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 2018-06-05
  • 1970-01-01
  • 2017-12-10
  • 1970-01-01
  • 2013-06-15
  • 2021-03-20
相关资源
最近更新 更多