copy file and show progress in ProgressDialog - Android User Interface

Android examples for User Interface:ProgressDialog

Description

copy file and show progress in ProgressDialog

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.ProgressDialog;
import android.content.Context;

public class Main {

    public static File copy1(Context context, String filename,
            String destfilename, ProgressDialog pd) {

        try {//from  w w  w  . j av a  2 s .  c  o m
            InputStream in = context.getAssets().open(filename);
            int max = in.available();
            if (pd != null) {
                pd.setMax(max);
            }

            File file = new File(destfilename);
            OutputStream out = new FileOutputStream(file);
            byte[] byt = new byte[1024];
            int len = 0;
            int total = 0;
            while ((len = in.read(byt)) != -1) {
                out.write(byt, 0, len);
                total += len;
                if (pd != null) {
                    pd.setProgress(total);
                }
            }
            out.flush();
            out.close();
            in.close();

            return file;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related Tutorials