0

Hi friends am new to android. I created an application form filling with 4 tabs and now I need a screen shot of the layout if its edit text is filled on button click of save. Is it possible to do in android, if not in any format but i want a copy of the fields with its image

Calendar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    //getScreenViewBitmap(vs);
    loadBitmapFromView(vs);
}
});
}
public static RelativeLayout createProgrammeView
(Context context, int width, int height, String title, String time) {
    RelativeLayout.LayoutParams params;


    RelativeLayout rlv = new RelativeLayout(context);
    params = new RelativeLayout.LayoutParams(width, height);
    rlv.setLayoutParams(params);
    rlv.setPadding(3, 3, 3, 3);
    //rlv.setBackgroundResource(R.drawable.background);


    TextView tv = new TextView(context);
    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    tv.setId(R.id.textView1);
    tv.setLayoutParams(params);
    tv.setGravity(Gravity.CENTER_VERTICAL);
    tv.setSingleLine(true);
    tv.setEllipsize(TruncateAt.END);
    tv.setTextColor(Color.parseColor("#fff"));
    tv.setTextSize(11);
    tv.setText(title);
    rlv.addView(tv);


    tv = new TextView(context);
    params = new RelativeLayout.LayoutParams(16, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.BELOW, R.id.textView1);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.setMargins(0, 4, 0, 0);
    tv.setId(R.id.time);
    tv.setLayoutParams(params);
    tv.setGravity(Gravity.CENTER_VERTICAL);
    tv.setSingleLine(true);
    tv.setEllipsize(null);
    tv.setTextColor(Color.parseColor("#fff"));
    tv.setTextSize(10);
    tv.setText(time);
    rlv.addView(tv);        

return rlv;
}
public static Bitmap loadBitmapFromView(RelativeLayout v) {
    Bitmap b = Bitmap.createBitmap(582, 136, Bitmap.Config.ARGB_8888);
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
      b.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

        System.out.println(b);

        File f = new File(Environment.getExternalStorageDirectory()
                                + File.separator + "test1111.jpeg");
        FileOutputStream fo;

            try {
                fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());


                fo.close();
            } catch (FileNotFoundException e) {

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

                e.printStackTrace();
            }
            System.out.println(f.exists());
    Canvas c = new Canvas(b);
    v.draw(c);
    return b;
}

Hi in the above code its a relative layout where i tried for the layout output as image but it shows the empty black image nothing is there but it creates a file. Also when i click there is a change in the view of layout

Manju
  • 67
  • 3
  • 11

1 Answers1

0

try this it may help you

 public static Bitmap loadBitmapFromView(View v) {
              Bitmap b = Bitmap.createBitmap( v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);                
              Canvas c = new Canvas(b);
              v.measure(v.getWidth(),v.getHeight());
              v.layout(0,0,v.getWidth(),v.getHeight());
              v.draw(c);
              return b;
        }
Youddh
  • 1,506
  • 4
  • 34
  • 51
  • java.lang.IllegalArgumentException: width and height must be > 0. ITs showing an error. How can I see the saved image. @Youddh – Manju Jul 09 '13 at 10:13
  • 2
    have u try to pass TabHost Layout in method ..? – Youddh Jul 09 '13 at 10:15
  • Please check the above code as how I used. Its done in normal layout as I want to know first its process. So I can put it in Tab Host and try. It shows the same error in the above code also as java.lang.IllegalArgumentException: width and height must be > 0 – Manju Jul 09 '13 at 10:53
  • 1
    u r passing R.id.utils need to pass v(View). – Youddh Jul 09 '13 at 11:23
  • Hi now am getting the screen shot but since i kept it on click of a button it is showing the button's image so how to get it for whole layout and save it for viewable. – Manju Jul 09 '13 at 12:03
  • Hi thanx a lot it works fine now but how i save that and view from gallery. – Manju Jul 09 '13 at 12:36
  • 2
    http://stackoverflow.com/questions/11846108/android-saving-bitmap-to-sd-card try this – Youddh Jul 10 '13 at 03:34
  • Hi please check the above code which i tried but it creates a file and not showing any image inside that file – Manju Jul 10 '13 at 06:07