Hey, friends in this blog I will tell you how you can download Image or a big file in Android Studio.
In fetching of an image, you can Fetch with volley Network Image library, Picasso, Glide.
the are popular API to fetch images from Server but if I want to Download an image from Server, there is way more Complex method in Picasso to Download file.
To simplify it Android have already Download Manager.
By the help of Download manager, you can download any image or big file
So it's the best way to download an image from Server.
Official Doc-ClickHere
Steps Todo-
1. First of all, Take the Internet and Storage permission By adding below lines in Manifest file in Android.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<Button | |
android:id="@+id/button" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="32dp" | |
android:layout_marginTop="292dp" | |
android:layout_marginEnd="32dp" | |
android:onClick="imageDownload" | |
android:text="Download" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.498" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</android.support.constraint.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
private static final String[] STORAGE_PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE}; | |
String uri="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?cs=srgb&dl=beauty-bloom-blue-67636.jpg&fm=jpg"; | |
int permissionExternalMemory; | |
DownloadManager mgr; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
verifyPermissions(); | |
} | |
private void verifyPermissions() { | |
// This will return the current Status | |
permissionExternalMemory = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE); | |
if(permissionExternalMemory != PackageManager.PERMISSION_GRANTED) | |
{ | |
// If permission not granted then ask for permission real time. | |
ActivityCompat.requestPermissions(MainActivity.this,STORAGE_PERMISSIONS,1); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void imageDownload(View view) { | |
if(permissionExternalMemory == PackageManager.PERMISSION_GRANTED) | |
{ | |
downloadFile(uri); | |
}else{ | |
verifyPermissions(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void downloadFile(String uRl) { | |
File direct = new File(Environment.getExternalStorageDirectory() | |
+ "/Test"); | |
if (!direct.exists()) { | |
direct.mkdirs(); | |
} | |
mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE); | |
Uri downloadUri = Uri.parse(uRl); | |
DownloadManager.Request request = new DownloadManager.Request( | |
downloadUri); | |
request.setAllowedNetworkTypes( | |
DownloadManager.Request.NETWORK_WIFI | |
| DownloadManager.Request.NETWORK_MOBILE) | |
.setAllowedOverRoaming(false).setTitle("Test") | |
.setDescription("Something useful. No, really.") | |
.setDestinationInExternalPublicDir("/Test", "activity.jpg"); | |
mgr.enqueue(request); | |
// Open Download Manager to view File progress | |
Toast.makeText(this, "Downloading...",Toast.LENGTH_LONG).show(); | |
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); | |
} | |
BroadcastReceiver receiver = new BroadcastReceiver () { | |
public void onReceive(Context context, Intent intent) { | |
String action = intent.getAction(); | |
if (action.equals(mgr.ACTION_DOWNLOAD_COMPLETE) ){ | |
Toast.makeText(MainActivity.this,"Successfully Downloaded",Toast.LENGTH_SHORT).show(); | |
unregisterReceiver(receiver); | |
finishActivity(99); | |
} | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.downloadfile; | |
import android.Manifest; | |
import android.app.DownloadManager; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.content.pm.PackageManager; | |
import android.net.Uri; | |
import android.os.Environment; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Toast; | |
import java.io.File; | |
public class MainActivity extends AppCompatActivity { | |
private static final String[] STORAGE_PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE}; | |
String uri="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?cs=srgb&dl=beauty-bloom-blue-67636.jpg&fm=jpg"; | |
int permissionExternalMemory; | |
DownloadManager mgr; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
verifyPermissions(); | |
} | |
private void verifyPermissions() { | |
// This will return the current Status | |
permissionExternalMemory = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE); | |
if(permissionExternalMemory != PackageManager.PERMISSION_GRANTED) | |
{ | |
// If permission not granted then ask for permission real time. | |
ActivityCompat.requestPermissions(MainActivity.this,STORAGE_PERMISSIONS,1); | |
} | |
} | |
public void imageDownload(View view) { | |
if(permissionExternalMemory == PackageManager.PERMISSION_GRANTED) | |
{ | |
downloadFile(uri); | |
}else{ | |
verifyPermissions(); | |
} | |
} | |
public void downloadFile(String uRl) { | |
File direct = new File(Environment.getExternalStorageDirectory() | |
+ "/QualityKg"); | |
if (!direct.exists()) { | |
direct.mkdirs(); | |
} | |
mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE); | |
Uri downloadUri = Uri.parse(uRl); | |
DownloadManager.Request request = new DownloadManager.Request( | |
downloadUri); | |
request.setAllowedNetworkTypes( | |
DownloadManager.Request.NETWORK_WIFI | |
| DownloadManager.Request.NETWORK_MOBILE) | |
.setAllowedOverRoaming(false).setTitle("Test") | |
.setDescription("Something useful. No, really.") | |
.setDestinationInExternalPublicDir("/Test", "activity.jpg"); | |
mgr.enqueue(request); | |
// Open Download Manager to view File progress | |
Toast.makeText(this, "Downloading...",Toast.LENGTH_LONG).show(); | |
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); | |
} | |
BroadcastReceiver receiver = new BroadcastReceiver () { | |
public void onReceive(Context context, Intent intent) { | |
String action = intent.getAction(); | |
if (action.equals(mgr.ACTION_DOWNLOAD_COMPLETE) ){ | |
Toast.makeText(MainActivity.this,"Successfully Downloaded",Toast.LENGTH_SHORT).show(); | |
unregisterReceiver(receiver); | |
finishActivity(99); | |
} | |
} | |
}; | |
} |
Download manager.
8. This is the DownloadManger function which is responsible for Download Image From Server-
Uri downloadUri = Uri.parse(uRl);
DownloadManager.Request request = new DownloadManager.Request( downloadUri);
9. If you have any problem in this procedure you can Download Source Code from the below link.
Download Code-ClickMe
0 Comments