Retrofit 2 file down/upload

前端 未结 5 1755
栀梦
栀梦 2020-12-13 00:54

I\'m trying to down/upload a file with retrofit 2 but can\'t find any tutorials examples on how to do so. My code for downloading is:

@GET(\"documents/checko         


        
5条回答
  •  一个人的身影
    2020-12-13 01:16

    You can refer tutorial for Image Download using Retrofit 2.0

    For the time being you can refer following functions for image download:

    void getRetrofitImage() {
    
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    
        RetrofitImageAPI service = retrofit.create(RetrofitImageAPI.class);
    
        Call call = service.getImageDetails();
    
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Response 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 part image download using Retrofit 2.0

    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;
        }
    }
    

    I hope it will help. All the best. Happy Coding :)

提交回复
热议问题