Android 截图程序实现 需要root权限 (调用linux 命令)

public class Screenshot extends Activity {
private static final String DATA_PATH = "/data/data/com.su.ScreenShot/";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        CopyAssets();
       
        Button btClose = (Button) findViewById(R.id.button1);  
        btClose.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View v) {  
            runRootCommand("chmod 777 /data/data/com.su.ScreenShot/gsnap");
            Log.v("2", "2---------------");
            runRootCommand("/data/data/com.su.ScreenShot/gsnap test.jpg /dev/graphics/fb0");//使用有root权限的命令 运行gsnap的c程序
            Log.v("3", "3---------------");
              }             
        }); 
    }
        
        
   
    public static boolean runRootCommand(String command) {
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
// nothing
}
}
return true;
}
    
    
   


public static boolean runCommand(String command) {
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
process.destroy();
} catch (Exception e) {
// nothing
}
}
return true;
}
private void CopyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {


}
for (int i = 0; i < files.length; i++) {
InputStream in = null;
OutputStream out = null;
try {
if (!(new File(DATA_PATH + files[i])).exists()) {
in = assetManager.open(files[i]);
out = new FileOutputStream(DATA_PATH + files[i]);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
}
} catch (Exception e) {
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}




}

其实主要还是人家的c程序 我只是写了几句shell命令



赚点豆豆~

C程序的地址 http://download.csdn.net/detail/sfshine/3781315

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值