android - 简单的根文件资源管理器问题

标签 android file permissions root explorer

您好,我正在开发一个简单的文件浏览器以将其包含在我的应用程序中,这一切都很好,但是我无法访问/data 等文件夹,我需要使用文件浏览器才能访问这些文件夹。我要求获得 root 访问权限,但我想我做错了其他事情,但我是新手,这只是我第二次尝试使用应用程序,所以我仍在学习。无论如何,有谁知道发生了什么事以及为什么我无法访问像/data 这样的文件夹,即使我已被授予 super 用户权限 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;
    }
}

最佳答案

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

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

关于android - 简单的根文件资源管理器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5636325/

相关文章:

android - picasso 图像未加载到 GridView 中

android - PlatformException(sign_in_failed,com.google.android.gms.common.api.ApiException : 10: , null)

c# - this.Property 和 C# .NET 中的 Property 的区别

java - 当我尝试将文件从 jar 内复制到磁盘时,为什么会收到 'nullfiles' ?

java - 使用自定义适配器将项目动态添加到 ListView 中的问题

java - Java File 实例存储在哪里?

unix - 目录路径变量是否应该以斜杠结尾?

postgresql - 如何通过 Amazon EC2 将 postgresql 数据移动到 Ubuntu 上的另一个目录?

cocoa - 仅显示一次密码提示 - 如何?

php - 用户权限树 : How to get there?