Android - screen capture

I need to take a screenshot and save a screenshot. I need to do this without using any connection to the PC or without turning off the phone. I need to do this whenever an event fires. For example, when an ad is shown in a game ... or when a game ends and shows ratings in a snake, etc. Can you tell me how I can do this. I saw some tutorilas and they gave the code, but this does not seem to work

private void getScreen() { View content = findViewById(R.id.layoutRoot); Bitmap bitmap = content.getDrawingCache(); File file = new File("/sdcard/test.png"); try { file.createNewFile(); FileOutputStream ostream = new FileOutputStream(file); bitmap.compress(CompressFormat.PNG, 100, ostream); ostream.close(); } catch (Exception e) { e.printStackTrace(); } } 
+1
source share
3 answers

Can you give more information about what does not work when running this code? Isn't that capturing what you want? Crash?

Make sure you modify R.id.layoutroot correctly using the root layout ... Also, it looks like this will work ...

 <com.example.android.snake.SnakeView android:id="@+id/snake" android:layout_width="match_parent" android:layout_height="match_parent" tileSize="24" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text" android:text="@string/snake_layout_text_text" android:visibility="visible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center_horizontal" android:textColor="#ff8888ff" android:textSize="24sp"/> </RelativeLayout> 

Edit ...

So, for example, if you use this layout, which you just put in there, you must change R.id.layout to R.id.snake , which is because of this line: android:id="@+id/snake" .

I don’t think there is an easy way to find / get the identifier of the β€œroot” view layout (if you want to take a screenshot of what the phone shows.

I just checked the launchpad and another application, it looks like most applications are placed in FrameLayout with id / content, so you can try using android.R.id.content , but there is no guarantee that this will work every time ...

0
source

Before calling getDrawingCache (), you must enable the cache before calling getDrawingCache ().

+1
source
 View ve = findViewById(R.id.loyout); ve.setDrawingCacheEnabled(true); Bitmap b = ve.getDrawingCache(); String extr = Environment.getExternalStorageDirectory().toString() + "/SaveCapture"; myPath = new File(extr); if (!myPath.exists()) { boolean result = myPath.mkdir(); Toast.makeText(this, result + "", Toast.LENGTH_SHORT).show(); } myPath = new File(extr, getString(R.string.app_name) + ".jpg"); Toast.makeText(this, myPath.toString(), Toast.LENGTH_SHORT).show(); FileOutputStream fos = null; try { fos = new FileOutputStream(myPath); b.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen"); } catch (FileNotFoundException e) { Log.e("Error", e + ""); } catch (Exception e) { Log.e("Error", e + ""); } 
+1
source

Source: https://habr.com/ru/post/904474/


All Articles