关于Android的root权限调用 空指针错误

Ronys 2012-03-18 01:32:29
前几天在做一个文件管理器,在打开非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;
}



虽然调用成功,但是还是空指针错误,如下:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
// loadApps();
rootCommand(rootPowerCommand);//调用获取root权限
initTool();
initFileList();
}

@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);// 打开文件夹。。。。。。。。。。。。。。。。接下面
}
}



/**
* 打开目录
*
* @param file
* */
public void openDir(File file) {
fileBroswer.current_path = file.getAbsolutePath();
fileBroswer.currentDir.setText(file.getAbsolutePath());
File[] files = file.listFiles();//得到的files竟然是空的,就是说虽然目录文件存在,但是你不能访问它,
data = fileBroswer.getData(files);//由此也就照成了空指针错误,为什么么?求解释啊。。。。。。
MyAdapter myAdapter = new MyAdapter(context, data);
fileBroswer.setListAdapter(myAdapter);
}


...全文
230 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ronys 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yiyaaixuexi 的回复:]
public final String rootPowerCommand = "chmod 777 /dev/block/mmcblk0";
这条命令并不是授权root权限命令,它的意思是把/dev/block/mmcblk0这个块设备的权限开放至777,即任何人都可以读写。
想要这条命令执行成功,必须有root权限。如果没有root权限,这条命令是无效的。根本无法更改为777。
[/Quote]
那要怎么办,,,用什么命令。。。。
Ronys 2012-03-19
  • 打赏
  • 举报
回复
问题是我色友他也是这样获取权限的,手机会自动提示是否允许root操作之类的,然后就选择允许,,,,可是我的导出的apk放到他手机。。。直接就是空指针错误。。。。
念茜 2012-03-18
  • 打赏
  • 举报
回复
public final String rootPowerCommand = "chmod 777 /dev/block/mmcblk0";
这条命令并不是授权root权限命令,它的意思是把/dev/block/mmcblk0这个块设备的权限开放至777,即任何人都可以读写。
想要这条命令执行成功,必须有root权限。如果没有root权限,这条命令是无效的。根本无法更改为777。

Ronys 2012-03-18
  • 打赏
  • 举报
回复
自己先顶下!偶是新手。。。。还请大家多多指点,,

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧