Downloading Files Using OKIO In Android

Android
 

Introduction

 
In this article, we will learn how to use OKIO Library to download files from the server in Android.
 

OKIO

 
OKIO is a library that complements java.io and java.nio to make it much easier to access, store, and process your data. Simply put, OKIO is a modern I/O API for Java. It is a component of OKHTTP. To know more visit the following link.
 
Reference
 
https://github.com/square/okio/blob/master/README.md  
OKIO was developed by SQUARE.
 
Coding Part
 
I have split this part into 3 steps as in the following.
  • Creating a New Project with Empty Activity.
  • Setting up the OKIO Library.
  • Implementation of downloading files.
 
Step 1 - Create a new project with Empty Activity
  1. Open Android Studio and Select Create a new project.
  2. Name the project as per your wish and select Empty activity.
     
    Android
      
  3. Click the “Finish” button to create a new project in Android Studio.
Step 2 - Setting up the OKIO Library
 
In this part, we will see how to set up the library for the project.
  1. Open your app level build.gradle file and add the following lines in dependencies
    compile 'com.squareup.okhttp3:okhttp:3.6.0'
     
    OKIO is a component of OkHttp. Therefore, we can use OkHttp to download any files from the server through URL.
  1. Then click “Sync Now” to set up your project.
Step 3 - Implementation of downloading files
 
In this part, we will see how to set up manifest for Permissions. Add the following lines to AndroidManifest..xml file.
  1. <uses-permission android:name="android.permission.INTERNET"/>  
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
DOWNLOADING IMAGE
 
The following code explains how to use OKIO with OKHTTP.
  1. Request request = new Request.Builder().url(imageLink).build();  
  2. new OkHttpClient().newCall(request).enqueue(new Callback() {@Override  
  3.     public void onFailure(Call call, IOException e) {}  
  4.   
  5.     @Override  
  6.     public void onResponse(Call call, Response response)  
  7.     throws IOException {  
  8.         if (!folder.exists()) {  
  9.             boolean folderCreated = folder.mkdir();  
  10.         }  
  11.         file = new File(folder.getPath() + "/savedImg.png");  
  12.         if (file.exists()) {  
  13.             boolean fileDeleted = file.delete();  
  14.             Log.v("fileDeleted", fileDeleted + "");  
  15.         }  
  16.         boolean fileCreated = file.createNewFile();  
  17.         BufferedSink sink = Okio.buffer(Okio.sink(file));  
  18.         sink.writeAll(response.body().source());  
  19.         sink.close();  
  20.     }  
  21. });  
Here, we are inputting the image url to OKHTTP Request Builder. The OkHttpClient makes a request to the server & outputs the respective files from the server.
 
OKIO is similar to Java I/O API. It has two important components -- BufferSource and BufferSink.
  1. BufferSource is like InputStream in Java I/O API. You can view any Source as an InputStream, and you can view any InputStream as a Source. It's similar for Sink and OutputStream
     
  2. BufferSink is like OutputStream in Java I/O API. You can view any Sink as an OutputStream, and you can view any OutputStream as a Sink.
The above example explains how to use the following code part used to save the file.
  1. ufferedSink sink = Okio.buffer(Okio.sink(file));  
  2. sink.writeAll(response.body().source());  
  3. sink.close();  
Here, the file name to be saved is given as input to BufferSink and writeAll reads the source of the response body of the given link, which saves the file with the given file name respectively.
 
Download Code
 
If this article was informative please do like and star the repo in GitHub. You can download the full sample code from GitHub.
 
Demo
 
You can find the demo video on YouTube


Similar Articles