Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

android - how to take screenshot programmatically and save it on gallery?

i would to know what is the code to take a screenshot of the current screen (after a press of a button) and save it on a gallery because I don't have a device with sd cards. So i would to save in the default gallery. thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  Bitmap bitmap;
  View v1 = findViewById(R.id.rlid);// get ur root view id
  v1.setDrawingCacheEnabled(true); 
  bitmap = Bitmap.createBitmap(v1.getDrawingCache());
  v1.setDrawingCacheEnabled(false);

This should do the trick.

For saving

  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
  File f = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "test.jpg")
  f.createNewFile();
  FileOutputStream fo = new FileOutputStream(f);
  fo.write(bytes.toByteArray()); 
  fo.close();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...