12

I'm trying to take a screenshot of the currently playing video. I'm trying with code that successfully takes a screenshot of web view but get not success in taking photo of currently playing video.

The code as follow for web view.

WebView w = new WebView(this);

w.setWebViewClient(new WebViewClient()
{
    public void onPageFinished(WebView view, String url)
    {
         Picture picture = view.capturePicture();
         Bitmap  b = Bitmap.createBitmap( picture.getWidth(),
         picture.getHeight(), Bitmap.Config.ARGB_8888);

         Canvas c = new Canvas( b );

         picture.draw( c );

         FileOutputStream fos = null;

         try {
             fos = new FileOutputStream( "/sdcard/yahoo_" +System.currentTimeMillis() + ".jpg" );

             if ( fos != null )
             {
                 b.compress(Bitmap.CompressFormat.JPEG, 90, fos );

                 fos.close();
             }

         } catch( Exception e )
         {
             //...
         }
    }
});

setContentView( w );

w.loadUrl( "http://www.yahoo.com");
skynet
  • 9,898
  • 5
  • 43
  • 52
milind
  • 960
  • 4
  • 12
  • 38
  • 1
    Unfortunately, I don't think this is possible in Gingerbread/Honeycomb. The video gets displayed in a separate Surface that can't be rendered into a Canvas/Bitmap combo. This might be possible with the TextureView APIs in Ice Cream Sandwich, though I'm not sure. – mportuesisf Nov 15 '11 at 18:18
  • may be u are right but still i need to do r&d for i m near to archive this, i get rootview of the screen but not get current view. – milind Nov 15 '11 at 19:53

3 Answers3

1

To expand on 66CLSjY's answer, FFmpegMediaMetadataRetriever has the same interface as MediaMetadataRetriever but it uses FFmpeg as the backend. If the default configuration won't work with your video format you can enable/disable codecs by recompiling. Here is some sample code:

FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(mUri);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_CODEC);
Bitmap b = getFrameAtTime(3000);
mmr.release();
William Seemann
  • 3,440
  • 10
  • 44
  • 78
0

try this it will gives bitmap for your app screen

View v = view.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
Sharad Mhaske
  • 1,103
  • 9
  • 19
0

This works for me:

First a method to convert your view into a bitmap

public static Bitmap getBitmapFromView(View view) {

    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) 
        bgDrawable.draw(canvas);
    else 
        canvas.drawColor(Color.WHITE);
    view.draw(canvas);
    return returnedBitmap;
}

Then save into the SD, for example:

static private boolean saveImage(Bitmap bm, String absolutePath)
{
    FileOutputStream fos = null;
    try 
    {
    String absolutePath = "your path"
        File file = new File(absolutePath);
        fos = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 100, fos); //PNG ignora la calidad
    } catch (IOException e) 
    {
        e.printStackTrace();
    } 
    finally 
    {
        try 
        {
            if (fos != null)
                fos.close();
        } 
        catch (Exception e)
        { 
            e.printStackTrace(); 
        }
    }
    return true;
}

Good Luck!

vlopezla
  • 95
  • 1
  • 15