How to execute "cd sdcard" command

by Karteek N » Thu, 22 Apr 2010 20:50:35 GMT


Sponsored Links
 Hi ,
I want to display what are the files and contents in sdcard through
application.
I am able to print the root folder.But when i execute cd sdcard
it throws me exception that permission is denied.
When i did the same thing using adb shell i am able to display the sdcard.
Is there any way to change permissions like sudo

I used the following code to display root folder

 Runtime rt = Runtime.getRuntime();
          try {
                   Process process;
                   process=rt.exec("ls");
                 try{
                          BufferedReader in =new BufferedReader(new
InputStreamReader(process.getInputStream()));

                         String line;
                         while ((line = in.readLine()) != null) {
                               Log.d("debug",line);
                                }
                          in.close();

                      } catch (Exception e) {
                              e.printStackTrace();
                             System.out.println(e.getMessage());
                       }


                   } catch (IOException e) {

                          e.printStackTrace();
                  }


But when change the above code like
  process=rt.exec("cd sdcard");
  process=rt.exec("ls");
Its throws exception

Any help please
Regards,
Karteek

--



How to execute "cd sdcard" command

by Mark Murphy » Thu, 22 Apr 2010 20:53:06 GMT


 


Why don't you use Java file I/O?

Forking processes to read the output of things like ls is wasteful of
CPU, wasteful of memory, wasteful of battery life, and slow, when you
have alternatives.

 http://exampledepot.com/egs/java.io/GetFiles.html 
 http://exampledepot.com/egs/java.io/TraverseTree.html 

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

_The Busy Coder's Guide to Android Development_ Version 3.0
Available!

--


Sponsored Links


How to execute "cd sdcard" command

by Bob Kerns » Fri, 23 Apr 2010 00:40:27 GMT


 Mark points out all the reasons you shouldn't even be trying to do
this.

But there's more! I assume you're going to take Mark's advice anyway,
but these are things that are likely to cause you trouble someday in
the future, so I'll point them out anyway.

First, you are trying to cd to some directory you have no reason to
believe exists. You don't have any idea what your current directory is
-- why should you expect it to have a 'sdcard' subdirectory? I expect
you meant "/sdcard" -- and that might work on your device. It might
not work on some other device, though. See this class for methods to
handle this cleanly:

 http://developer.android.com/intl/de/reference/android/os/Environment.html 
(You'll want to make use of this with Mark's advice, too).

And even if you did manage to cd to a directory named 'sdcard' -- it
would have NO EFFECT WHATSOEVER on the next command you execute!

You ran a shell and told it to do 'cd sdcard'. When it's done with
that, it exits. You no longer have a shell with that working
directory. Now you try to run 'ls' in a new shell. This new shell
doesn't care what you did in the old shell. It is NOT going to be
executing in your sdcard directory.

You could put these two commands into a .sh file, and execute that.
But what's the point? You could supply the same path directly to 'ls'.
Or even better, use the Java facilities for accessing the filesystem
and avoid all kinds of bugs you'd probably introduce -- like, for
example, handling spaces in filenames, etc.



>



How to execute "cd sdcard" command

by karteek » Fri, 23 Apr 2010 08:59:52 GMT


 hanks Marks,Bob.
Your help is really appreciated,
I will follow java i/o.

On Apr 22, 9:39pm, Bob Kerns <r...@acm.org> wrote:
> >



Other Threads

1. [WTI] pasar malam sudah sembuh

Info saja, setelah setiap pagi mencoba dan tidak bisa diakses, pagi ini
pasar malam kembali bisa diakses

-- 
===============
"

2. Emulator crashes (Because of concurrency, I think)

Hello,

I've been wrestling for a few days with concurrency related to setting
up different threads for a game app.
I have made many versions, but they have all failed. At this point, I
just want to get a basic concurrent setup going. The worst part is
that it is the emulator that crashes, so DDMS reports nothing;
therefore I'm pretty clueless as to where the issue is.

The following code shows an activity (class Main), that calls classes
SceneManager, which creates a thread to be used to game logic stuff. A
3rd class, StatusChannel, is (will be) used to communicate status
information between the different threads (Eventually, there will also
be a OpenGL rendering thread).

The emulator crashes at different times. It may run for 20 seconds or
for 5 minutes.

The setContentView(R.layout.main) in the Activity class just the set
basic layout that Eclipse creates.

I've commented out the usage of Node (Created in the activity and
Accessed in SceneManager)

Here are the 3 classes.

Sorry for the code length.

############################# BEGIN Activity Class
###################################

public class Main extends Activity {
        private SceneManager mSceneManager;
        private volatile Node mSceneGraph = new Node();
        private volatile Status mStatusChannel = new Status();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d("-- Main", "onCreate()");
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        // Holds the scene assets, such as the stage,
        // the agents, camera, etc.
        mSceneManager = new SceneManager(mSceneGraph,
mStatusChannel);
        mSceneManager.onCreate();
    }

    @Override
    protected void onResume() {
        Log.d("-- Main", "onResume()");
        super.onResume();
        mSceneManager.onResume();
    }

    @Override
    protected void onPause() {
        Log.d("-- Main", "onPause()");
        super.onPause();
        mSceneManager.onPause();
    }

    @Override
    protected void onDestroy() {
        Log.d("-- Main", "onDestroy()");
        super.onDestroy();
        mSceneManager.onDestroy();
    }
}
################################## END Activity Class
######################################



################################# START SceneManager Class
##############################
public class SceneManager implements Runnable{
        private Thread mThread;
        private volatile Status mStatusChannel;
        private volatile Node mSceneGraph;

        private volatile long mMillis = 0;
        private volatile PrepareVisitor mPrepareVisitor;
        private volatile int mStatus = Status.UNKNOWN_STATUS;

        SceneManager(Node sceneGraph, Status statusChannel) {
                mPrepareVisitor = new PrepareVisitor();
                mStatusChannel = statusChannel;
                mSceneGraph = sceneGraph;
        mMillis = SystemClock.uptimeMillis();
                mThread  = new Thread(this);
                mThread.setName("LogicThread");
                mStatusChannel.setSceneManagerStatus(Status.READY_STATUS);
        }

        public void onCreate() {
                Log.d("-- SceneManager", "onCreate()...");
                // This will start the thread in a paused state.
                mThread.start();
        }

        public void onResume() {
                Log.d("-- SceneManager", "onResume()...");

                // Unpause the status manager, if it is currently paused.
                if (mStatusChannel.getSceneManagerStatus() == 
Status.PAUSED_STATUS)
{
                        
mStatusChannel.setSceneManagerStatus(Status.READY_STATUS);
                }
        }

        public void onPause() {
                Log.d("-- SceneManager", "onPause()...");
                if (mStatusChannel.getSceneManagerStatus() != 
Status.UNKNOWN_STATUS)
{
                        
mStatusChannel.setSceneManagerStatus(Status.PAUSED_STATUS);
                }
        }

        public void onDestroy() {
                mStatusChannel.setSceneManagerStatus(Status.QUIT_STATUS);
                try {
                        mThread.join();
                }
                catch (InterruptedException e) {
                        Log.d("-- SceneManager", "InterruptedException");
                }
        }

        /**
         *  This method should not be called by clients of this class.
         */
        @Override
        public void run() {
                Log.d("-- SceneManager", "Called...");

                // Main logic loop.
                outer: while (true) {

                        // How much time has elapsed since last call.
                        long timeDelta = SystemClock.uptimeMillis() - mMillis;

                        switch (mStatus) {
                        case Status.READY_STATUS:
                                //mPrepareVisitor.go(mSceneGraph, timeDelta);
                                break;
                        case Status.PAUSED_STATUS:
                                break;
                        case Status.QUIT_STATUS:
                                break outer;
                        case Status.UNKNOWN_STATUS:
                                int renderStatus = 
mStatusChannel.getRendererStatus();
                                if (renderStatus == Status.READY_STATUS) {
                                        
mStatusChannel.setSceneManagerStatus(Status.READY_STATUS);
                                }
                                break;
                        }

                        mStatus = mStatusChannel.getSceneManagerStatus();

                // Update the time.
                        mMillis = SystemClock.uptimeMillis();
                }
        }
}

################################# END SceneManager Class
##############################

############################## START Status Class
###################################
public class Status {

        /* Generic Statuses */
        public final static int UNKNOWN_STATUS = 0;
        public final static int READY_STATUS = 1;
        public final static int PAUSED_STATUS = 2;
        public final static int QUIT_STATUS = 3;

        /* Current statuses values */
        private int mSceneManagerStatus = UNKNOWN_STATUS ;
        private int mRendererStatus     = UNKNOWN_STATUS ;


        public synchronized int getSceneManagerStatus() {
                return mSceneManagerStatus;
        }

        public synchronized int getRendererStatus() {
                return mRendererStatus;
        }

        public synchronized void setSceneManagerStatus(int status) {
                mSceneManagerStatus = status;
        }

        public synchronized void setRendererStatus(int status) {
                mRendererStatus = status;
        }
}
############################## END Status Class
###################################

-- 

3. Preferable OS environment?

4. Programmatically discover ContentProviders in the current application

5. SQLite3 Queries: Order By with Negative numbers backwards?

6. Closed WTI: Pasar Malem sudah buka lagi

7. [WTI] Aplikasi Push Mail