viernes, 13 de febrero de 2015

Android: Save screenshot to file, write to an internal file

Save screenshot to file
private void saveScreenshot() {

  View view = (View) topMostView;
  view.setDrawingCacheEnabled(true);
  Bitmap b = view.getDrawingCache();
  String extr = Environment.getExternalStorageDirectory().toString();
  Date date = Calendar.getInstance().getTime();
  DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
  String now = formatter.format(date);
  File myPath = new File(extr, "screenshot_" + now + ".jpg");
  FileOutputStream fos = null;
  try {
   fos = new FileOutputStream(myPath);
   b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
   fos.flush();
   fos.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
Write to internal file (no permission are required)
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;

try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  outputStream.write(string.getBytes());
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

No hay comentarios:

Publicar un comentario