Android FileBrowser v.2.0

Tutorials with advanced 'difficulty' and more Lines of Code.

Re: Android FileBrowser v.2.0

Postby dgrafix » Tue Mar 13, 2012 12:27 pm

Hi,

EDIT: JUST REALISED I POSTED THIS IN V2 THREAD. I MEANT TO POST IT IN THE V1 THREAD SORRY :oops:

Thanks for this! I have had to make a few fixes to it to get it to work.

I have marked all my changes with "//Fixed"

I also notice you are calling your main activity one thing and then changing it to another (could be confusing for some ;)

Code: Select all
      //........................................................
        //Fixed not sure what was going on with the dialogue, so i wrote one.
        //........................................................
        private void browseTo(final File aDirectory){
                if (aDirectory.isDirectory()){
                        this.currentDirectory = aDirectory;
                        fill(aDirectory.listFiles());
                }else{
                        OnClickListener okButtonListener = new OnClickListener(){
                                // @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                        try {
                                                // Lets start an intent to View the file, that was clicked...
                                                Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW,
                                                                Uri.parse("file://"
                                                                                + aDirectory.getAbsolutePath()));
                                                startActivity(myIntent);
                                        } catch (Exception e) {
                                                e.printStackTrace();
                                        }
                                }
                        };
                        doDialogue(this,"Question","Do you want to open that file?\n"
                              + aDirectory.getName(),
                              "OK", "Cancel",
                              okButtonListener);
                }
        }

        //........................................................
        //Added this method. Now dialogue is reusable.
        //........................................................
        private void doDialogue(Context context, String title, String message, String positivetext,  String negativetext,OnClickListener okButtonListener){
              AlertDialog.Builder dialog = new AlertDialog.Builder(context);
              dialog.setTitle(Html.fromHtml( title ));
              dialog.setMessage(Html.fromHtml(message));
              dialog.setPositiveButton(positivetext, okButtonListener);
              dialog.setNegativeButton(negativetext, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                 }
              });
              dialog.show();

                 
         
      }

       
        //........................................................
        //Fixed files comes up as null when browsing system folder
        //........................................................
      private void fill(File[] files) {
                this.directoryEntries.clear();
                if (files==null){
                   files = new File[0];
                }
                // Add the "." and the ".." == 'Up one level'
                try {
                        Thread.sleep(10);
                } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }
                this.directoryEntries.add(".");
               
                if(this.currentDirectory.getParent() != null)
                        this.directoryEntries.add("..");
               
                switch(this.displayMode){
                        case ABSOLUTE:
                                for (File file : files){
                                        this.directoryEntries.add(file.getPath());
                                }
                                break;
                        case RELATIVE: // On relative Mode, we have to add the current-path to the beginning
                                int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length();
                                for (File file : files){
                                        this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght));
                                }
                                break;
                }
               
                ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
                                R.layout.file_row, this.directoryEntries);
               
                this.setListAdapter(directoryList);
        }

        //........................................................
        //Fixed getItem method not present, why not just use the ID available
        //........................................................
       @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
                int selectionRowID = (int) id;
                String selectedFileString = this.directoryEntries.get(selectionRowID);
                if (selectedFileString.equals(".")) {
                        // Refresh
                        this.browseTo(this.currentDirectory);
                } else if(selectedFileString.equals("..")){
                        this.upOneLevel();
                } else {
                        File clickedFile = null;
                        switch(this.displayMode){
                                case RELATIVE:
                                        clickedFile = new File(this.currentDirectory.getAbsolutePath()
                                                                                                + this.directoryEntries.get(selectionRowID));
                                        break;
                                case ABSOLUTE:
                                        clickedFile = new File(this.directoryEntries.get(selectionRowID));
                                        break;
                        }
                        if(clickedFile != null)
                                this.browseTo(clickedFile);
                }
        }


:D
dgrafix
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Feb 20, 2011 11:10 pm

Top

Re: Android FileBrowser v.2.0

Postby tuff » Wed Mar 28, 2012 2:29 pm

Hi,

I just used your tutorial and it perfectly works but I have a question:

is it possible to resize the list or lunch it in a little Layout because I would add some buttons...

Its maybe simple but its my first application.

Thanks
tuff
Freshman
Freshman
 
Posts: 2
Joined: Wed Mar 28, 2012 2:07 pm

Re: Android FileBrowser v.2.0

Postby tuff » Mon Apr 02, 2012 1:23 pm

A little Up ^^
tuff
Freshman
Freshman
 
Posts: 2
Joined: Wed Mar 28, 2012 2:07 pm

Re:

Postby rasam550 » Sat Aug 18, 2012 8:50 am

smalltalk wrote:Hello +-(plusminus) :D
Do you miss .this.deleteFile(String) is an abstract method. I used but android state error. I'm going to 'raise level' your version. I want to make it(Browser of you) be able to copy,paste,move,delete...I think this is rather "difficult" :( because class File don't support much, but I 'll try. Thanks for tutorial, you're great :). If you know much about File class in android. Please reply me fast :wink:


I m also working on this actually. And i am very new in this field. Can u help to find out how to make my browser able to cut, copy,paste,show file or folder details and create new folders ? If u provide any tutorial on any site plz also help me by providing that. I have an university project on this. So this is very urgent to me. Thanks in advance. My yahoo mail id: rasam_hossain@yahoo.com
rasam550
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Aug 03, 2012 7:22 pm

Re: Android FileBrowser v.2.0

Postby sheteshrikant54 » Mon Oct 08, 2012 6:36 am

after runing the app i m getting follo error
[2012-10-08 10:49:55 - adb] ADB server didn't ACK
[2012-10-08 10:49:55 - adb] * failed to start daemon *

can any one help as android development is new for me..??
thank u..
sheteshrikant54
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Oct 08, 2012 6:30 am

Re: Android FileBrowser v.2.0

Postby RapsFan » Mon Feb 25, 2013 3:33 am

Hi great tutorials! Is there a way to lock the user to a home directory? I created a directory called My Webs and I got the liat to load the files and subdirectories in My Webs, but using the upOneLevel button the user can navigate to the filesystem root. This is what I've tried.

Code: Select all

private String rootdir = Environment.getExternalStorageDirectory() + "/My Webs";
protected void onListItemClick(ListView l, View v, int position, long id) {
                ...
                if (selectedFileString.equals(getString(R.string.up_one_level)) && currentDirectory.getAbsoluteFile().toString()!=rootdir) {
                        this.upOneLevel();
                }
}
RapsFan
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Feb 25, 2013 3:27 am

Top
Previous

Return to Advanced Tutorials

Who is online

Users browsing this forum: Google [Bot] and 5 guests