简单的Root文件浏览器问题

时间:2011-04-12 13:53:39

标签: android file permissions root explorer

您好我正在使用一个简单的文件浏览器来包含在我的应用程序中这一切正常但是我无法访问文件浏览器中需要能够访问的/ data这样的文件夹。我呼吁root访问,但我想我正在做其他错误,但我是新的这只是我现在在应用程序的第二次尝试,所以我还在学习。无论如何有人知道怎么了什么,为什么我不能访问像/ data这样的文件夹,即使我已被授予超级用户权限o如果我将/ data的权限更改为777我可以查看里面的东西,但这听起来不是喜欢我应该做的事情我的意思是root explorer可以在文件夹不是777时查看它?感谢您的帮助

package com.app.package;

import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainMethod extends ListActivity {

 private List<String> item = null;
 private List<String> path = null;
 private String root="/";
 private TextView myPath;
 private static Process rt;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!(requestRoot())) {
            Toast.makeText(MainMethod.this, "Could Not Get Root!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainMethod.this, "Root Found!", Toast.LENGTH_SHORT).show();
        }
        setContentView(R.layout.main);
        myPath = (TextView)findViewById(R.id.path);
        getDir(root);
    }

    private void getDir(String dirPath)
    {
     myPath.setText("Location: " + dirPath);

     item = new ArrayList<String>();
     path = new ArrayList<String>();

     File f = new File(dirPath);
     File[] files = f.listFiles();

     if(!dirPath.equals(root))
     {

      item.add(root);
      path.add(root);

      item.add("../");
      path.add(f.getParent());

     }

     for(int i=0; i < files.length; i++)
     {
       File file = files[i];
       path.add(file.getPath());
       if(file.isDirectory())
        item.add(file.getName() + "/");
       else
        item.add(file.getName());
     }

     ArrayAdapter<String> fileList =
      new ArrayAdapter<String>(this, R.layout.row, item);
     setListAdapter(fileList);
    }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {

  File file = new File(path.get(position));

  if (file.isDirectory())
  {
   if(file.canRead())
    getDir(path.get(position));
   else
   {
    new AlertDialog.Builder(this)
    .setIcon(R.drawable.icon)
    .setTitle("[" + file.getName() + "] folder can't be read!")
    .setPositiveButton("OK", 
      new DialogInterface.OnClickListener() {

       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
       }
      }).show();
   }
  }
  else
  {
   new AlertDialog.Builder(this)
    .setIcon(R.drawable.icon)
    .setTitle("[" + file.getName() + "]")
    .setPositiveButton("OK", 
      new DialogInterface.OnClickListener() {

       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
       }
      }).show();
  }
 }
    private static boolean requestRoot()
    {
        try {
            rt = Runtime.getRuntime().exec("su");
            DataOutputStream dos = new DataOutputStream(rt.getOutputStream());
            dos.writeBytes("exit\n");
            dos.flush();
            try {
                rt.waitFor();
            } catch (InterruptedException e) {
                return false;
            }
        } catch (IOException e) {
            return false;
        }
        return true;
    }
}

2 个答案:

答案 0 :(得分:2)

当您调用“su”时,您正在创建一个运行该命令的新进程,因此在运行exec(“su”)之后,您将拥有一个具有SU权限的新进程。你不能给你已经运行的应用程序SU权利!恐怕!

我不确定其他应用程序是如何做到的,但您可以运行“su -l ls / data”,然后从该进程的输出流中读取。

答案 1 :(得分:0)

我最终搞清楚了我基本上只是调用了ls(目录)并解析了它的输出