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;
}
}
I did end up figuring it out i basically just called ls (directory) and parsed it’s output