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) {
android caching image bitmap screenshot
user577732 Oct 14 2018-11-11T00: 00Z
source share