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

发布于 2024-10-31 15:45:47 字数 3733 浏览 3 评论 0原文

您好,我正在开发一个简单的文件浏览器,将其包含在我的应用程序中,这一切都工作正常,但是我无法访问 /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;
    }
}

Hi i'm working on a simple file browser to include in my app this all works fine however i can not access folders such as /data which i would need to be able to access with the file browser. I called for root access but i guess i'm doing something else wrong but i'm new this is only my second try at an app now so i'm still learning. Anyway does anyone know what's up and why i can't access folders like /data even though i have been granted super user permissions o and if i change the permissions of /data to 777 i can view the stuff inside but that doesn't sound like something i should have to do i mean root explorer can view it when the folder is not 777? Thank you for any help

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油饼 2024-11-07 15:45:47

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

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

When you call "su" you're creating a new process that runs that command, so after running exec("su") you have a new process with SU rights. You can't give your already running application SU rights I'm afraid!

I'm not sure how other applications do this, but you can run "su -l ls /data" and then read from the outputstream of the process.

聊慰 2024-11-07 15:45:47

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

I did end up figuring it out i basically just called ls (directory) and parsed it's output

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文