2011-04-12 31 views
1

嗨我正在一個簡單的文件瀏覽器,以包括在我的應用程序這一切工作正常,但我不能訪問文件夾,如/數據,我需要能夠訪問與文件瀏覽器。我呼籲root訪問,但我想我做了一些其他的錯誤,但我是新的這只是我現在在應用程序的第二次嘗試,所以我仍然在學習。無論如何,有人知道這是怎麼回事,爲什麼我不能訪問文件夾/數據,即使我已被授予超級用戶權限o,如果我更改/數據的權限到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; 
    } 
} 

回答

0

我確實最終弄明白了我基本上只是稱爲ls(目錄)並解析它的輸出

+0

請問您可以發佈更正的工作代碼。只是想看看如何從根目錄中列出文件。 – xmen 2013-06-09 02:00:44

+0

你能分享一些額外的信息嗎?我也有同樣的問題... – 2013-07-16 06:26:57

2

當你叫「蘇」你要創建一個運行該命令,因此運行EXEC(「蘇」)後你有SU權限的新進程的新進程。你不能給你已經運行的應用程序SU權利,我害怕!

我不確定其他應用程序如何執行此操作,但可以運行「su -l ls/data」,然後從過程的輸出流中讀取數據。