简体   繁体   中英

android Screen Capture

i want to capture the video screen as image from videoview in android..my code is given below.its create the image but that image contains only blackscreen in sdcard.how to capture the videoview as image while playing.

public class VidePlayerActivity extends Activity {
/** Called when the activity is first created. */
public String fname;
public int n=1;
LinearLayout screen;
Timer timer;
Bitmap bmScreen;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
    Button sr=(Button)findViewById(R.id.screenrec);
    screen=(LinearLayout)findViewById(R.id.screen);
    VideoView videoView=(VideoView)findViewById(R.id.videoView1);
    videoView.setVideoPath("sdcard/test.mp4");
    MediaController mediaController=new MediaController(this);
    videoView.setMediaController(mediaController);
    sr.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                    screen.setDrawingCacheEnabled(true);
                    bmScreen = screen.getDrawingCache();
                    saveImage(bmScreen);
        }
    });
    }
 protected void saveImage(Bitmap bmpscreen3) {

        //Toast.makeText(ScreenCaptureActivity.this, "saved", Toast.LENGTH_SHORT).show();
        File myDir=new File("/sdcard/Video_images");
          myDir.mkdirs();

           fname = "img"+ n +".jpg";
          File file = new File (myDir, fname);

        try {
            FileOutputStream ostream = new FileOutputStream(file);
            bmpscreen3.compress(CompressFormat.JPEG, 100, ostream);
            ostream.flush();
            ostream.close();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
}

Something similar has already been posted to stackoverflow, check below might help you out.

Screen Capture in android

Also one here:

Programmatic screencapture on mobile device

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM