Pada tulisan sebelumnya gw udah menulis contoh untuk upload image menggunakan progressbar lebih spesifiknya progressbar dialog. Nah pada tulisan kali ini ane akan coba menulis contoh download, tapi tak hanya image tapi bisa juga untuk file yang bertipe apapun. Contoh aplikasi nya ga berbeda jauh dengan tulisan sebelumnya, hanya tampilan sederhana yang ditampilkan. Untuk tampilan yang lain yang lebih bagus bisa dikostum sesuai selera anda..xixixi. Berikut penampakan aplikasinya.
Berikut kode-kode yang dapat ditulisakan untuk membuat aplikasi seperti yang tampak pada gambar di atas.
AndroidManifest.xml
Main.xml
FileDownloader.java
package com.hendi.example.download; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.AsyncTask; import android.os.Environment; import android.util.Log; import android.widget.Toast; public class FileDownloader extends AsyncTaskDownloadActivity.java{ private ProgressDialog progressDialog; private Context context; private String url; private HttpPost post; private String msg = "Download failed"; private long fileSize; public FileDownloader(Context context, String url) { this.context = context; this.url = url; } private void showProgressDialog() { progressDialog = new ProgressDialog(context); progressDialog.setMax(100); progressDialog.setMessage("Downloading File"); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setProgress(0); progressDialog.setButton("Cancel", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { msg = "download aborted"; post.abort(); } }); progressDialog.show(); } @Override protected void onPreExecute() { showProgressDialog(); } @Override protected Boolean doInBackground(Void... params) { DefaultHttpClient client = new DefaultHttpClient(); post = new HttpPost(url); try { HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); fileSize = entity.getContentLength(); String fileName = Environment.getExternalStorageDirectory().toString() + File.separator.toString(); String[] splitUrl = url.split("/"); fileName += splitUrl[splitUrl.length - 1]; Log.i("FileDownloader", "File Name : " + fileName); InputStream is = entity.getContent(); OutputStream os = new FileOutputStream(fileName); final int buffer_size = 1024; byte[] bytes = new byte[buffer_size]; int total = 0; for (;;) { int count = is.read(bytes, 0, buffer_size); total += count; publishProgress("" + (int) ((total * 100) / fileSize)); if (count == -1) break; os.write(bytes, 0, count); } os.close(); msg = "Download success"; return true; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; } @Override protected void onPostExecute(Boolean result) { progressDialog.dismiss(); showToast(); } @Override protected void onProgressUpdate(String... progress) { int percent = Integer.parseInt(progress[0]); progressDialog.setProgress(percent); } private void showToast() { Toast.makeText(context, msg, 3).show(); } }
package com.hendi.example.download; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class DownloadActivity extends Activity { /** Called when the activity is first created. */ Button bDownload; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bDownload = (Button) findViewById(R.id.btn_download); bDownload.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { FileDownloader downloader = new FileDownloader(DownloadActivity.this, "http://10.22.51.150/hendi/download/me.jpg"); downloader.execute(); } }); } }
Demikian tulisan gw kali ini. Untuk mencoba aplikasi ini langsung di eclipse, dapat donlot disini.
Tidak ada komentar:
Posting Komentar