Retrofit API to retrieve a png image

后端 未结 8 1254
我在风中等你
我在风中等你 2020-12-02 20:32

Hi I am new to Retrofit framework for Android. I could get JSON responses from REST services using it but I don\'t know how to download a png using retrofit. I am trying to

相关标签:
8条回答
  • 2020-12-02 21:16

    I hope following code will help you:

    Include following function inside MainActivity.java:

    void getRetrofitImage() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    
        RetrofitImageAPI service = retrofit.create(RetrofitImageAPI.class);
    
        Call<ResponseBody> call = service.getImageDetails();
    
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Response<ResponseBody> response, Retrofit retrofit) {
    
                try {
    
                    Log.d("onResponse", "Response came from server");
    
                    boolean FileDownloaded = DownloadImage(response.body());
    
                    Log.d("onResponse", "Image is downloaded and saved ? " + FileDownloaded);
    
                } catch (Exception e) {
                    Log.d("onResponse", "There is an error");
                    e.printStackTrace();
                }
    
            }
    
            @Override
            public void onFailure(Throwable t) {
                Log.d("onFailure", t.toString());
            }
        });
    }
    

    Following is the file handling code for image:

    private boolean DownloadImage(ResponseBody body) {
    
            try {
                Log.d("DownloadImage", "Reading and writing file");
                InputStream in = null;
                FileOutputStream out = null;
    
                try {
                    in = body.byteStream();
                    out = new FileOutputStream(getExternalFilesDir(null) + File.separator + "AndroidTutorialPoint.jpg");
                    int c;
    
                    while ((c = in.read()) != -1) {
                        out.write(c);
                    }
                }
                catch (IOException e) {
                    Log.d("DownloadImage",e.toString());
                    return false;
                }
                finally {
                    if (in != null) {
                        in.close();
                    }
                    if (out != null) {
                        out.close();
                    }
                }
    
                int width, height;
                ImageView image = (ImageView) findViewById(R.id.imageViewId);
                Bitmap bMap = BitmapFactory.decodeFile(getExternalFilesDir(null) + File.separator + "AndroidTutorialPoint.jpg");
                width = 2*bMap.getWidth();
                height = 6*bMap.getHeight();
                Bitmap bMap2 = Bitmap.createScaledBitmap(bMap, width, height, false);
                image.setImageBitmap(bMap2);
    
                return true;
    
            } catch (IOException e) {
                Log.d("DownloadImage",e.toString());
                return false;
            }
        }
    

    This is done using Android Retrofit 2.0. I hope it helped you.

    Source: Image Download using Retrofit 2.0

    0 讨论(0)
  • 2020-12-02 21:17

    Retrofit is a REST library, you can use Retrofit only to get image URL but for displaying Image you should use Picasso: http://square.github.io/picasso/

    0 讨论(0)
提交回复
热议问题