Pertama silakan membuat Project Android baru di Eclipse atau Android Studio, tambahkan dua buah Button.
Jangan lupa menambahkan permission untuk koneksi ke Internet dan untuk Read maupun Write ke SDCard.
package com.amijaya.download_file_online_to_sdcard;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button button1;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
URL u;
u = new URL("http://192.168.43.247/launcher.png");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
//FileOutputStream f = new FileOutputStream(new File(root,"Video.mp4"));
FileOutputStream f = new FileOutputStream(new File("//sdcard/","launcher.png"));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 ) {
f.write(buffer,0, len1);
}
f.close();
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
new DefaultHttpClient().execute(new HttpGet("http://contohprogram.com/demo/ahp-android-sqlite/AHP-Android.apk"))
.getEntity().writeTo(
new FileOutputStream(new File("//sdcard/","AHP-Android.apk")));
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Download 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Download 2" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.amijaya.download_file_online_to_sdcard" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-sdk android:minSdkVersion="8" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.amijaya.download_file_online_to_sdcard.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>
Hasilnya setelah dijalankan maka akan tampil seperti ini :
Jika diklik tombol tersebut maka file akan terdownload ke SDCard, silakan dicek, apabila berhasil maka file akan terdownload di SDCard paling luar.
Jika anda mengalami crash, kemungkinan anda memakai Perangkat Mobile/HP dan/atau SDK OS Kitkat, Lollipop atau Marshmallow. Untuk ini anda harus menambahkan Background Thread atau Ansychronous Thread di proses downloadnya. Contoh penggunaan background thread dapat dilihat di artikel ini http://cariprogram.blogspot.co.id/2014/06/aplikasi-android-menggunakan-thread-background-process-mengambil-data-dari-web-server-http.html
Project selengkapnya dapat anda download disini.
Semoga berguna ^_^
Tidak ada komentar:
Posting Komentar