In this article we are going to discuss about how to download image from url and save that image into sdcard by using following concepts
* URLConnection
* DataInputStream
* DataOutputStream
* FileOutputStream
Coming to coding
Create a sample Project
1) Layout File
create a file in res/layout folder
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/downloadbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="27sp"
android:padding="3dp"
android:text="Download Image" />
</RelativeLayout>
* URLConnection
* DataInputStream
* DataOutputStream
* FileOutputStream
Coming to coding
Create a sample Project
1) Layout File
create a file in res/layout folder
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/downloadbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="27sp"
android:padding="3dp"
android:text="Download Image" />
</RelativeLayout>
2) AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.downloadimage"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3) MainActivity.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private ProgressDialog pDialog;
Boolean isInternetPresent = false;
ConnectionDetector cd;
Button downloadbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadbtn=(Button)findViewById(R.id.downloadbtn);
pDialog = new ProgressDialog(this);
cd = new ConnectionDetector(getApplicationContext());
downloadbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
download();
}else
{
Toast.makeText(getApplicationContext(), "Please connect to internet", 4000).show();
}
}
});
}
protected void download() {
new DownloadImageFromURL().execute();
}
class DownloadImageFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.setMessage("Downloading...");
pDialog.setCancelable(false);
pDialog.show();
}
/**
* Downloading file in background thread
*/
@Override
protected String doInBackground(String... f_url) {
String imagepath="provide your image url";
try {
final File filename1 = new File(imagepath);
String imagename = filename1.getName();
URL u = new URL(imagepath);
URLConnection conn = u.openConnection();
int contentLength = conn.getContentLength();
DataInputStream stream = new DataInputStream(u.openStream());
byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();
String dir = Environment.getExternalStorageDirectory() + File.separator + "Download";
//create folder
File folder = new File(dir); //folder name
folder.mkdirs();
File folderpath = new File(folder + File.separator + imagename);
DataOutputStream fos = new DataOutputStream(new FileOutputStream(folderpath));
fos.write(buffer);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
return "sucess";
}
@Override
protected void onPostExecute(String resp) {
super.onPostExecute(resp);
pDialog.dismiss();
}
}
}
Finally run your application
OUTPUT:
we can display image from sdcard into ImageView Just refer this here