Android編程中中root權限的獲取
前幾天在做一個文件管理器,在打開非sdcard下的目錄文件時碰到了空指針的錯誤,色友說要獲取root權限才能訪問其他需root權限的文件夾,于是用了下面的方法獲取權限,
public final String rootPowerCommand = "chmod 777 /dev/block/mmcblk0";// 授權root權限命令
/**
* 授權root用戶權限
*
* @param command
* */
public boolean rootCommand(String command) {
Process process = null;
DataOutputStream dos = null;
try {
process = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(process.getOutputStream());
dos.writeBytes(command+"\n");
dos.writeBytes("exit\n");
dos.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (dos != null) {
dos.close();
}
process.destroy();
} catch (Exception e) {
}
}
return true;
}
雖然調用成功,但是還是空指針錯誤,如下:
<p>@Overridepublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
// loadApps();
rootCommand(rootPowerCommand);//調用獲取root權限
initTool();
initFileList();
}</p> <p>@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) this.getListAdapter()
.getItem(position);
FileBean fileBean = (FileBean) map.get("icon");
Log.v("--------path---------", fileBean.getPath());
File file = new File(fileBean.getPath());//此處路勁fileBean.getPath()經調試得到是存在的目錄,如我點擊root文件夾得到/root
if (!file.isDirectory()) {
fileControl.openFile(file);// 打開文件
} else {
fileDirControl.openDir(file);// 打開文件夾。。。。。。。。。。。。。。。。接下面
}
}</p>
/*
打開目錄
@param file
/
public void openDir(File file) {
fileBroswer.current_path = file.getAbsolutePath();
fileBroswer.currentDir.setText(file.getAbsolutePath());
File[] files = file.listFiles();//得到的files竟然是空的,就是說雖然目錄文件存在,但是你不能訪問它,
</span> data = fileBroswer.getData(files);//由此也就照成了空指針錯誤,為什么么?求解釋啊。。。。。。
MyAdapter myAdapter = new MyAdapter(context, data);
fileBroswer.setListAdapter(myAdapter);
}
本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!