save Web Content To File - Android Network

Android examples for Network:URL

Description

save Web Content To File

Demo Code


//package com.java2s;

import android.content.Context;

import android.net.Uri;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static void saveToFile(Context context, Uri uri, String filePath) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {//from   w  w w.j a  v  a  2  s  .  c  o  m
            bis = new BufferedInputStream(context.getContentResolver()
                    .openInputStream(uri));
            bos = new BufferedOutputStream(new FileOutputStream(filePath,
                    false));
            byte[] buffer = new byte[1024];

            bis.read(buffer);
            do {
                bos.write(buffer);
            } while (bis.read(buffer) != -1);
        } catch (IOException ioe) {

        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {

            }
        }
    }
}

Related Tutorials