【问题标题】:Simple Root File Explorer Question简单的根文件资源管理器问题
【发布时间】:2011-04-12 13:53:38
【问题描述】:

您好,我正在开发一个简单的文件浏览器以包含在我的应用程序中,这一切都很好,但是我无法访问 /data 之类的文件夹,我需要能够使用文件浏览器访问这些文件夹。我要求获得 root 访问权限,但我想我做错了其他事情,但我是新手,这只是我现在第二次尝试应用程序,所以我仍在学习。无论如何,有谁知道发生了什么以及为什么我无法访问 /data 之类的文件夹,即使我已被授予超级用户权限 o 如果我将 /data 的权限更改为 777,我可以查看里面的内容,但这听起来不像就像我应该做的事情我的意思是当文件夹不是 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;
    }
}

【问题讨论】:

    标签: android file permissions root explorer


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 能否请您发布更正后的工作代码。只想看看你如何列出根目录中的文件。
      猜你喜欢
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多