0

I had designed my android layout with some textviews and listview,after loading screen i just wanted to take screen shot of that layout and i have to save it on my device.Is it possible or not.

golla raghu
  • 39
  • 1
  • 7
  • Is passible to take screenshot – kalidoss rajendran May 26 '15 at 11:08
  • 1
    see http://stackoverflow.com/questions/2661536/how-to-programatically-take-a-screenshot-on-android – flafoux May 26 '15 at 11:09
  • some guys down vote this question,why you are discourage the freshers. – kalidoss rajendran May 26 '15 at 11:10
  • 1
    I didnt downvoted it, but I can clearly see the reason why other people would do it; the question does not show any kind of effort or whatsoever. – AlvaroSantisteban May 26 '15 at 11:11
  • the given sample not working fine,its throwing some error like source not found @ this line bitmap = Bitmap.createBitmap(v1.getDrawingCache()); and also i used View v1 = getWindow().getDecorView().getRootView(); instead of View v1 = mCurrentUrlMask.getRootView(); because its throwing some error – golla raghu May 26 '15 at 11:20
  • @AlvaroSantisteban i had tried different examples but they didnt work for me,thats why i posted here with my requirement,it is difficult to post all the examples what i had tried – golla raghu May 26 '15 at 11:22

2 Answers2

1
Bitmap bitmap;
View v1 = findViewById(R.id.rlid);// get ur root view id
v1.setDrawingCacheEnabled(true); 
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

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();

dont forget to give permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Vinay Jayaram
  • 1,030
  • 9
  • 29
0

From How to programatically take a screenshot on Android?

Here is the sample code:

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   
// create bitmap screen capture
Bitmap bitmap;
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
  fout = new FileOutputStream(imageFile);
  bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
  fout.flush();
  fout.close();

} catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

For this you need this permission in manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Community
  • 1
  • 1
King of Masses
  • 18,405
  • 4
  • 60
  • 77
  • Insted of mCurrentUrlMask you can use like this View v1 = getWindow().getDecorView().getRootView(); – King of Masses May 26 '15 at 12:47
  • I had tried but its throwing an error NullException at line bitmap = Bitmap.createBitmap(v1.getDrawingCache()); and i changed these lines in your code String mPath=Environment.getExternalStorageDirectory().toString() + "/" +"test.png"; and File imageFile = new File(mPath); – golla raghu May 26 '15 at 13:38