0

I have a LinearLayout and I am want to save the contents of that view as an image. I have it half working.

File imageFile;
// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/a.png";   

// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
Bitmap bitmap;
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.PNG, 10, fout);
    fout.flush();
    fout.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

With the above code it will save a copy of the view, but only what's seen on the device screen. I have more information on the view that I want saved. The code above from here and this will only save the information seen on the screen as an image. I want all the information saved, even the information that is not on the screen (where you need to scroll to see).

How can I achieve that?

Community
  • 1
  • 1
Howli
  • 12,291
  • 19
  • 47
  • 72
  • "where you need to scroll to see" -- scroll what? A `ListView`? A `GridView`? A `ScrollView`? A `WebView`? A `MapView`? Something else? – CommonsWare Sep 16 '14 at 00:00
  • To see the rest of the information. It is a tableview inside a linearlayout – Howli Sep 16 '14 at 00:13
  • You cannot scroll "a tableview inside a linearlayout". Yet you claim that what you are missing is "where you need to scroll to see". – CommonsWare Sep 16 '14 at 00:15
  • you can set clipChildren to false to parent view – Mohammad Ersan Sep 16 '14 at 00:16
  • @commonsware, there is a list view populating that tableview (my message was cut off for some reason). – Howli Sep 16 '14 at 00:27
  • My guess is that you will need to scroll the `ListView` yourself, take pictures as you go, and stitch the results together yourself. The other rows do not exist until you scroll them into view, so you cannot take pictures of them until you do. – CommonsWare Sep 16 '14 at 00:30
  • @CommonsWare, I was hoping to have an option to allow the user to save the information as an image. So with the layout I have I won't be able to do that? – Howli Sep 16 '14 at 21:58
  • Only if you assemble it manually. – CommonsWare Sep 16 '14 at 21:59

2 Answers2

1

Another option is to use:

Bitmap b = Bitmap.createBitmap(width, height....)
v1.setLayoutParams // Full width and height of content
Canvas c = new Canvas(b);
v1.draw(c); // You now have full bitmap
saveBitmap(b);
Zaid Daghestani
  • 8,555
  • 3
  • 34
  • 44
0

Run a measure/layout pass on it and draw it to a canvas. Suppose your parent was called "view" and was a vertical LinearLayout:

view.measure(someWidth, MeasureSpec.UNSPECIFIED);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

Canvas c = new Canvas(bitmap);

view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.draw(c);
Jim
  • 833
  • 6
  • 13