save URI To File with buffered stream - Android android.net

Android examples for android.net:Uri

Description

save URI To File with buffered stream

Demo Code

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

public class Main{
    public static void saveToFile(Context context, Uri uri, String filePath) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {//from  ww  w.  j  a  v  a2  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