无题
public class MainActivity extends Activity { /** Called when the activity is first created. */ private static final String TAG ="llsskk"; private Button btnShort; ImageView image = null; public static String SCREEN_SHOTS_LOCATION = Environment.getExternalStorageDirectory().getPath(); private Context mContext=this; private int mLCDWidth; private int mLCDHeight; private static final int PIXEL_BYTES = 4; private Bitmap mBitmap=null; private DataOutputStream dos=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); WindowManager mWm = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)); Point DisplaySize = new Point(); mWm.getDefaultDisplay().getSize(DisplaySize); mLCDWidth = DisplaySize.x; mLCDWidth += 8; // fusion3(SGH-I337), maybe plus 8 pixel. mLCDHeight = DisplaySize.y; image = (ImageView)findViewById(R.id.image); btnShort = (Button) findViewById(R.id.btnShots); btnShort.setOnClickListener(new Button.OnClickListener() { private Bitmap bitmao; public void onClick(View v) { try { Log.v(TAG, "click!!"); bitmao = takeScreenShotBySec(); image.setImageBitmap(bitmao); } catch (Exception e) { // TODO Auto-generated catch block Log.e("tag",TAG); } } }); } public boolean root() { try { // 执行su变更用户身份为root Process process = Runtime.getRuntime().exec("su"); dos = new DataOutputStream(process.getOutputStream()); } catch (Exception e) { e.printStackTrace(); return false; } return true; } protected Bitmap takeScreenShotBySec() { // TODO Auto-generated method stub byte[] bytestream = new byte[mLCDWidth * mLCDHeight * PIXEL_BYTES]; InputStream mFIS = null; if(root()){ try { dos.writeBytes("chmod 777 /dev/graphics/fb0\n"); dos.flush(); } catch (IOException e) { e.printStackTrace(); } } try { mFIS = new FileInputStream(new File("/dev/graphics/fb0")); mFIS.read(bytestream); if (mBitmap != null) mBitmap.recycle(); Log.v(TAG, "take!!"); mBitmap = Bitmap.createBitmap(mLCDWidth, mLCDHeight, Config.ARGB_8888); Log.v(TAG, mBitmap.toString()); for (int i = 0; i < bytestream.length; i = i + PIXEL_BYTES) { byte tem = bytestream[i]; bytestream[i] = bytestream[i + 2]; bytestream[i + 2] = tem; } Buffer buffer = ByteBuffer.wrap(bytestream); mBitmap.copyPixelsFromBuffer(buffer); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Log.v(TAG, e.toString()); } finally{ if(mFIS!=null){ try { mFIS.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return mBitmap; } }