Android launches software screenshot

Firstly, I am writing a root application, so root permissions are not a problem. I searched and searched and found a lot of code that never worked for me, that's what I put together so far and sorting works. When I say sorta, I mean that it does the image on my /sdcard/test.png, however the file has 0 bytes and obviously cannot be viewed.

public class ScreenShot extends Activity{ View content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.blank); content = findViewById(R.id.blankview); getScreen(); } private void getScreen(){ 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(); } } } 

Any help on how I can take a screen shot in android via code would be greatly appreciated!

=== EDIT ===

The following is all that I use, the image is made on my SD card and is no longer 0 bytes, but the whole thing is black, there is nothing on it. I attached the action to my search button, so when I’m somewhere on my phone, I click on the search for a long time and it should take a screenshot, but I just get a black image? Everything is set transparently, so I think it should capture everything that is on the screen, but I just turn black

manifest

 <activity android:name=".extras.ScreenShot" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:noHistory="true" > <intent-filter> <action android:name="android.intent.action.SEARCH_LONG_PRESS" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> 

XML

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00000000" android:id="@+id/screenRoot"> </LinearLayout> 

Screenshot class

 public class ScreenShot extends Activity{ View content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.screenshot); content = findViewById(R.id.screenRoot); ViewTreeObserver vto = content.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { content.getViewTreeObserver().removeGlobalOnLayoutListener(this); getScreen(); } }); } private void getScreen(){ View view = content; View v = view.getRootView(); v.setDrawingCacheEnabled(true); Bitmap b = v.getDrawingCache(); String extr = Environment.getExternalStorageDirectory().toString(); File myPath = new File(extr, "test.jpg"); 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) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finish(); } } 
+44
android caching image bitmap screenshot
Oct 14 2018-11-11T00:
source share
6 answers

Here you go ... I used this:

 View v = view.getRootView(); v.setDrawingCacheEnabled(true); Bitmap b = v.getDrawingCache(); String extr = Environment.getExternalStorageDirectory().toString(); File myPath = new File(extr, getString(R.string.free_tiket)+".jpg"); 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) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } 

v iz root layout ... just a point;)))

+26
Oct. 14 '11 at 5:45
source share
— -

For the next reader of this question -

The easiest way to do this is by drawing your eyes on the canvas -

pass the main link to this method -

  Bitmap file = save(layout); Bitmap save(View v) { Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); v.draw(c); return b; } 
+4
Jan 22 '15 at 17:18
source share

I think you need to wait until the layout is fully drawn. Use ViewTreeObserver to get a callback when the layout is fully drawn.

On your onCreate add this code ... Just call getScreen from inside onGlobalLayout () ..

 ViewTreeObserver vto = content.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { content.getViewTreeObserver().removeGlobalOnLayoutListener(this); getScreen(); } }); 

I asked a somewhat similar question once ... Please see my question, which explains how to take a screenshot in android. Hope this helps.

+1
Oct. 14 '11 at 3:21
source share
 public class MainActivity extends Activity { Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Bitmap bitmap = takeScreenshot(); saveBitmap(bitmap); } }); } public Bitmap takeScreenshot() { View rootView = findViewById(android.R.id.content).getRootView(); rootView.setDrawingCacheEnabled(true); return rootView.getDrawingCache(); } public void saveBitmap(Bitmap bitmap) { File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png"); FileOutputStream fos; try { fos = new FileOutputStream(imagePath); bitmap.compress(CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { Log.e("GREC", e.getMessage(), e); } catch (IOException e) { Log.e("GREC", e.getMessage(), e); } } } 

Do not forget to give permission to write to external storage!

+1
Apr 04 '15 at 5:27
source share

You take a screenshot of How it ........

 View view = findViewById(R.id.rellayout); view.getRootView(); String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { File picDir = new File(Environment.getExternalStorageDirectory()+ "/name"); if (!picDir.exists()) { picDir.mkdir(); } view.setDrawingCacheEnabled(true); view.buildDrawingCache(true); Bitmap bitmap = view.getDrawingCache(); // Date date = new Date(); String fileName = "mylove" + ".jpg"; File picFile = new File(picDir + "/" + fileName); try { picFile.createNewFile(); FileOutputStream picOut = new FileOutputStream(picFile); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), (int)(bitmap.getHeight()/1.2));//Optional boolean saved = bitmap.compress(CompressFormat.JPEG, 100, picOut); if (saved) { Toast.makeText(getApplicationContext(), "Image saved to your device Pictures "+ "directory!", Toast.LENGTH_SHORT).show(); } else { //Error } picOut.close(); } catch (Exception e) { e.printStackTrace(); } view.destroyDrawingCache(); } else { } 
0
Dec 23 '13 at 5:38
source share

My problem was with "TargetApi (23)", which is necessary if your minSdkVersion is given below.

So, I have permission to request with the following snippet

 protected boolean shouldAskPermissions() { return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1); } @TargetApi(23) protected void askPermissions() { String[] permissions = { "android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE" }; int requestCode = 200; requestPermissions(permissions, requestCode); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // ... if (shouldAskPermissions()) { askPermissions(); } } 
0
Mar 16 '17 at 22:58
source share



All Articles