How to make a capture screen app on Android

2019-08-20 07:33发布

问题:

I want to make an app which can capture device screen without root as: https://play.google.com/store/apps/details?id=com.icecoldapps.screenshotultimate

But I can't find the solution. Anyone help me?

Thank you very much.

回答1:

Programatically you can do so by executing the below command using adb:

adb shell /system/bin/screencap -p /sdcard/img.png

However to do the same from an application you can use the below method:

Process sh = Runtime.getRuntime().exec("su", null,null); //getting superuser permission to access /system/bin
OutputStream  os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII")); //executing the command
os.flush();
os.close();
sh.waitFor();

In /sdcard/ you will have img.png which will be your screen shot.



回答2:

From the FAQ of the app:

No “Capture Method” available for my device!

This is normal if your device isn't rooted. It's the way how the Android security model works; it just doesn't allow taking a screenshot on some devices. However, you can enable screenshot functionality on your device by enabling it through a computer. To see how you can do this go to “Screenshot Ultimate” > “Settings” > “Capture methods” > “No capture method help”. You can email the manual to yourself so you can complete it on your computer (Windows, Linux or Mac OSX).



回答3:

Check if this can help you out. The following code will help you to capture the screen of a particular Layout

RelativeLayout screenShotLayout=(RelativeLayout)findViewById(R.id.ScreenShotLayout);

Bitmap Bitmapimage=Bitmap.createBitmap(screenShotLayout.getWidth(),screenShotLayout.getHeight(), Config.ARGB_8888);
screenShotLayout.setDrawingCacheEnabled(true);
screenShotLayout.buildDrawingCache();
Bitmapimage=screenShotLayout.getDrawingCache();
//Bitmapimage is the screenshot of the layout. Do your stuff with it


回答4:

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   

// create bitmap screen capture
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
    fout = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
    fout.flush();
    fout.close();

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

To access the files :

Uri uri = Uri.fromFile(new File(mPath));