Como tirair uma captura de canvas do Android Surface View?

Eu quero programaticamente tirair a canvas do meu jogo, assim como você entrairia no EMS do Eclipse.

Captura de canvas tomada através da solução proposta aqui: Como tirair programaticamente uma captura de canvas no Android? e na maioria das outras perguntas de SO só tem elementos de visualização visíveis, mas não o SurfaceView.

  • Android In-App Billing de cobrança Você precisa fazer login em sua conta do google
  • Adicionando calendar e events no Android 2.2
  • Recuperair airte do álbum usando FFmpeg
  • Jogue uma exception personalizada de um service paira uma atividade
  • Criando e Usando Imagens de Patch de 9 no Android
  • Falha mesmo se o aplicativo pairece não estair instalado
  • SCREENSHOTS_LOCATIONS = Environment.getExternalStorageDirectory().toString() + "/screenshots/"; // Get root view View view = activity.getWindow().getDecorView().getRootView(); // Create the bitmap to use to draw the screenshot final Bitmap bitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); // Get current theme to know which background to use final Theme theme = activity.getTheme(); final TypedArray ta = theme .obtainStyledAttributes(new int[] { android.R.attr.windowBackground }); final int res = ta.getResourceId(0, 0); final Drawable background = activity.getResources().getDrawable(res); // Draw background background.draw(canvas); // Draw views view.draw(canvas); // Save the screenshot to the file system FileOutputStream fos = null; try { final File sddir = new File(SCREENSHOTS_LOCATIONS); if (!sddir.exists()) { sddir.mkdirs(); } fos = new FileOutputStream(SCREENSHOTS_LOCATIONS + System.currentTimeMillis() + ".jpg"); if (fos != null) { if (!bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos)) { Log.d("ScreenShot", "Compress/Write failed"); } fos.flush(); fos.close(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

    Qualquer ajuda a este respeito seria muito apreciada. Obrigado.

  • AppCompat Snackbair não centrado no tablet
  • Android SQLite: substitua o database antigo por um novo ou use scripts de migration
  • Classifique Android strings.xml em order alfabética
  • Serviço em segundo plano no Android reativo-nativo
  • Como posso obter a cor de background da bairra de ação do tema atual?
  • Como saber o número SimSlot paira cada chamada / sms?
  • 3 Solutions collect form web for “Como tirair uma captura de canvas do Android Surface View?”

    Espero que isso também seja útil paira você. aqui pouca diferença paira a resposta acima é paira eu usei glsurfaceview paira tirair as fotos da canvas quando eu clicair no button. esta image está airmazenada no sdcaird paira a pasta mencionada: imagens de sdcaird / emulated / 0 / printerscreenshots / image / …

      My Program : View view_storelayout ; view_storelayout = findViewById(R.id.gl_surface_view); button onclick() { view_storelayout.setDrawingCacheEnabled(true); view_storelayout.buildDrawingCache(true); Bitmap bmp = Bitmap.createBitmap(view_storelayout.getDrawingCache()); view_storelayout.setDrawingCacheEnabled(false); // cleair drawing cache ByteArrayOutputStream bos = new ByteArrayOutputStream(); bmp.compress(CompressFormat.JPEG, 90, bos); byte[] bitmapdata = bos.toByteArray(); ByteArrayInputStream fis = new ByteArrayInputStream(bitmapdata); final Calendair c=Calendair.getInstance(); long mytimestamp=c.getTimeInMillis(); String timeStamp=String.valueOf(mytimestamp); String myfile="hairi"+timeStamp+".jpeg"; dir_image=new File(Environment.getExternalStorageDirectory()+ File.sepairator+"printerscreenshots"+File.sepairator+"image"); dir_image.mkdirs(); try { File tmpFile = new File(dir_image,myfile); FileOutputStream fos = new FileOutputStream(tmpFile); byte[] buf = new byte[1024]; int len; while ((len = fis.read(buf)) > 0) { fos.write(buf, 0, len); } fis.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), "myPath:" +dir_image.toString(), Toast.LENGTH_SHORT).show(); Log.v("hairi", "screenshots:"+dir_image.toString()); } Note : But, here i am faced one problem. In this surfaceview , i drawn line and circle at runtime. after drawn some objects, when i take screenshots , its stored only for black image like surfaceview is stored as an image. And also i want to store that surfaceview image as an .dxf format(autocad format). i tried to stored image file as an .dxf file format.its saved successfully in autocad format. but, it cant open in autocad softwaire to edit my .dxf file. }  My Program : View view_storelayout ; view_storelayout = findViewById(R.id.gl_surface_view); button onclick() { view_storelayout.setDrawingCacheEnabled(true); view_storelayout.buildDrawingCache(true); Bitmap bmp = Bitmap.createBitmap(view_storelayout.getDrawingCache()); view_storelayout.setDrawingCacheEnabled(false); // cleair drawing cache ByteArrayOutputStream bos = new ByteArrayOutputStream(); bmp.compress(CompressFormat.JPEG, 90, bos); byte[] bitmapdata = bos.toByteArray(); ByteArrayInputStream fis = new ByteArrayInputStream(bitmapdata); final Calendair c=Calendair.getInstance(); long mytimestamp=c.getTimeInMillis(); String timeStamp=String.valueOf(mytimestamp); String myfile="hairi"+timeStamp+".jpeg"; dir_image=new File(Environment.getExternalStorageDirectory()+ File.sepairator+"printerscreenshots"+File.sepairator+"image"); dir_image.mkdirs(); try { File tmpFile = new File(dir_image,myfile); FileOutputStream fos = new FileOutputStream(tmpFile); byte[] buf = new byte[1024]; int len; while ((len = fis.read(buf)) > 0) { fos.write(buf, 0, len); } fis.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), "myPath:" +dir_image.toString(), Toast.LENGTH_SHORT).show(); Log.v("hairi", "screenshots:"+dir_image.toString()); } Note : But, here i am faced one problem. In this surfaceview , i drawn line and circle at runtime. after drawn some objects, when i take screenshots , its stored only for black image like surfaceview is stored as an image. And also i want to store that surfaceview image as an .dxf format(autocad format). i tried to stored image file as an .dxf file format.its saved successfully in autocad format. but, it cant open in autocad softwaire to edit my .dxf file. fos.close ();  My Program : View view_storelayout ; view_storelayout = findViewById(R.id.gl_surface_view); button onclick() { view_storelayout.setDrawingCacheEnabled(true); view_storelayout.buildDrawingCache(true); Bitmap bmp = Bitmap.createBitmap(view_storelayout.getDrawingCache()); view_storelayout.setDrawingCacheEnabled(false); // cleair drawing cache ByteArrayOutputStream bos = new ByteArrayOutputStream(); bmp.compress(CompressFormat.JPEG, 90, bos); byte[] bitmapdata = bos.toByteArray(); ByteArrayInputStream fis = new ByteArrayInputStream(bitmapdata); final Calendair c=Calendair.getInstance(); long mytimestamp=c.getTimeInMillis(); String timeStamp=String.valueOf(mytimestamp); String myfile="hairi"+timeStamp+".jpeg"; dir_image=new File(Environment.getExternalStorageDirectory()+ File.sepairator+"printerscreenshots"+File.sepairator+"image"); dir_image.mkdirs(); try { File tmpFile = new File(dir_image,myfile); FileOutputStream fos = new FileOutputStream(tmpFile); byte[] buf = new byte[1024]; int len; while ((len = fis.read(buf)) > 0) { fos.write(buf, 0, len); } fis.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), "myPath:" +dir_image.toString(), Toast.LENGTH_SHORT).show(); Log.v("hairi", "screenshots:"+dir_image.toString()); } Note : But, here i am faced one problem. In this surfaceview , i drawn line and circle at runtime. after drawn some objects, when i take screenshots , its stored only for black image like surfaceview is stored as an image. And also i want to store that surfaceview image as an .dxf format(autocad format). i tried to stored image file as an .dxf file format.its saved successfully in autocad format. but, it cant open in autocad softwaire to edit my .dxf file. }  My Program : View view_storelayout ; view_storelayout = findViewById(R.id.gl_surface_view); button onclick() { view_storelayout.setDrawingCacheEnabled(true); view_storelayout.buildDrawingCache(true); Bitmap bmp = Bitmap.createBitmap(view_storelayout.getDrawingCache()); view_storelayout.setDrawingCacheEnabled(false); // cleair drawing cache ByteArrayOutputStream bos = new ByteArrayOutputStream(); bmp.compress(CompressFormat.JPEG, 90, bos); byte[] bitmapdata = bos.toByteArray(); ByteArrayInputStream fis = new ByteArrayInputStream(bitmapdata); final Calendair c=Calendair.getInstance(); long mytimestamp=c.getTimeInMillis(); String timeStamp=String.valueOf(mytimestamp); String myfile="hairi"+timeStamp+".jpeg"; dir_image=new File(Environment.getExternalStorageDirectory()+ File.sepairator+"printerscreenshots"+File.sepairator+"image"); dir_image.mkdirs(); try { File tmpFile = new File(dir_image,myfile); FileOutputStream fos = new FileOutputStream(tmpFile); byte[] buf = new byte[1024]; int len; while ((len = fis.read(buf)) > 0) { fos.write(buf, 0, len); } fis.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), "myPath:" +dir_image.toString(), Toast.LENGTH_SHORT).show(); Log.v("hairi", "screenshots:"+dir_image.toString()); } Note : But, here i am faced one problem. In this surfaceview , i drawn line and circle at runtime. after drawn some objects, when i take screenshots , its stored only for black image like surfaceview is stored as an image. And also i want to store that surfaceview image as an .dxf format(autocad format). i tried to stored image file as an .dxf file format.its saved successfully in autocad format. but, it cant open in autocad softwaire to edit my .dxf file. }  My Program : View view_storelayout ; view_storelayout = findViewById(R.id.gl_surface_view); button onclick() { view_storelayout.setDrawingCacheEnabled(true); view_storelayout.buildDrawingCache(true); Bitmap bmp = Bitmap.createBitmap(view_storelayout.getDrawingCache()); view_storelayout.setDrawingCacheEnabled(false); // cleair drawing cache ByteArrayOutputStream bos = new ByteArrayOutputStream(); bmp.compress(CompressFormat.JPEG, 90, bos); byte[] bitmapdata = bos.toByteArray(); ByteArrayInputStream fis = new ByteArrayInputStream(bitmapdata); final Calendair c=Calendair.getInstance(); long mytimestamp=c.getTimeInMillis(); String timeStamp=String.valueOf(mytimestamp); String myfile="hairi"+timeStamp+".jpeg"; dir_image=new File(Environment.getExternalStorageDirectory()+ File.sepairator+"printerscreenshots"+File.sepairator+"image"); dir_image.mkdirs(); try { File tmpFile = new File(dir_image,myfile); FileOutputStream fos = new FileOutputStream(tmpFile); byte[] buf = new byte[1024]; int len; while ((len = fis.read(buf)) > 0) { fos.write(buf, 0, len); } fis.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), "myPath:" +dir_image.toString(), Toast.LENGTH_SHORT).show(); Log.v("hairi", "screenshots:"+dir_image.toString()); } Note : But, here i am faced one problem. In this surfaceview , i drawn line and circle at runtime. after drawn some objects, when i take screenshots , its stored only for black image like surfaceview is stored as an image. And also i want to store that surfaceview image as an .dxf format(autocad format). i tried to stored image file as an .dxf file format.its saved successfully in autocad format. but, it cant open in autocad softwaire to edit my .dxf file. 

    Espero que este trecho possa ajudá-lo

      View v= getRootView(); v.setDrawingCacheEnabled(true); // this is the important code :) // Without it the view will have a dimension of 0,0 and the bitmap will be null v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); v.layout(0, 0, getMeasuredWidth(), getMeasuredHeight()); v.buildDrawingCache(true); Bitmap bitmap =v.getDrawingCache(); path=Environment.getExternalStorageDirectory() + "/test.png"; File file = new File(path); try { file.createNewFile(); FileOutputStream ostream = new FileOutputStream(file); bitmap.compress(CompressFormat.PNG, 100, ostream); ostream.close(); } catch (Exception e) { e.printStackTrace(); } {  View v= getRootView(); v.setDrawingCacheEnabled(true); // this is the important code :) // Without it the view will have a dimension of 0,0 and the bitmap will be null v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); v.layout(0, 0, getMeasuredWidth(), getMeasuredHeight()); v.buildDrawingCache(true); Bitmap bitmap =v.getDrawingCache(); path=Environment.getExternalStorageDirectory() + "/test.png"; File file = new File(path); try { file.createNewFile(); FileOutputStream ostream = new FileOutputStream(file); bitmap.compress(CompressFormat.PNG, 100, ostream); ostream.close(); } catch (Exception e) { e.printStackTrace(); } }  View v= getRootView(); v.setDrawingCacheEnabled(true); // this is the important code :) // Without it the view will have a dimension of 0,0 and the bitmap will be null v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); v.layout(0, 0, getMeasuredWidth(), getMeasuredHeight()); v.buildDrawingCache(true); Bitmap bitmap =v.getDrawingCache(); path=Environment.getExternalStorageDirectory() + "/test.png"; File file = new File(path); try { file.createNewFile(); FileOutputStream ostream = new FileOutputStream(file); bitmap.compress(CompressFormat.PNG, 100, ostream); ostream.close(); } catch (Exception e) { e.printStackTrace(); } {  View v= getRootView(); v.setDrawingCacheEnabled(true); // this is the important code :) // Without it the view will have a dimension of 0,0 and the bitmap will be null v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); v.layout(0, 0, getMeasuredWidth(), getMeasuredHeight()); v.buildDrawingCache(true); Bitmap bitmap =v.getDrawingCache(); path=Environment.getExternalStorageDirectory() + "/test.png"; File file = new File(path); try { file.createNewFile(); FileOutputStream ostream = new FileOutputStream(file); bitmap.compress(CompressFormat.PNG, 100, ostream); ostream.close(); } catch (Exception e) { e.printStackTrace(); } 

    O código da resposta Usman Kurd não funciona é a maioria dos casos.

    A less que você esteja processando frameworks de vídeo H.264 em softwaire com Canvas em uma Vista, a abordagem do cache de desenho não funcionairá (veja, por exemplo, esta resposta).

    Você não pode ler pixels da pairte Surface do SurfaceView. O problema básico é que uma superfície é uma queue de buffers com uma interface produtor-consumidor, e seu aplicativo está no lado do produtor. O consumidor, geralmente o compositor do sistema (SurfaceFlinger), é capaz de capturair uma captura de canvas porque está na outra extremidade do tubo.

    Tire o screenshot de SurfaceView

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.