save Content from Uri To File - Android android.net

Android examples for android.net:Uri

Description

save Content from Uri To File

Demo Code

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

import android.content.Context;
import android.net.Uri;

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 av  a 2s  .com
      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