Android FileBrowser v.2.0

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

Size of SD Card?

Postby Shibbs » Fri Sep 05, 2008 3:25 pm

Hi +-,

Can we determine the size of the emmualted sdCard though our Java File API's.
i know its supported in 1.6 like

file.getTotalSpace()
file.getFreeSpace() etc....

but i can't get Android compile your FileBrowser files using java 1.6 sdk.
it just don't recognize file.getTotalSpace() api.

Your code runs well for java 1.5 & 1.6, but it shows compile errors the moment i keep any java 1.6 specific api's.

I have updated the project settings & Windows / Preferences of Eclipse so that it uses the java 1.6 sdk.
Even the JAVA_HOME & PATH System variables are updated to point to java 1.6 sdk.
and java -version & javac -version returns 1.6.

Does Android support's java 1.6 at all or not ?
Is there any way we could find the total space available in SD Card through code?

Your help is much appreciated.

Thanks,
Shibbs
Shibbs
Freshman
Freshman
 
Posts: 5
Joined: Fri Sep 05, 2008 3:14 pm

Top

Postby cgreen » Fri Sep 05, 2008 4:26 pm

android uses its own runtime, so youre on the wrong track using jre 1.6
the android runtime won't work with any imported jre packages, so you'll have to check the android specific packages to solve you problem.
localhost is the android emulator
User avatar
cgreen
Experienced Developer
Experienced Developer
 
Posts: 56
Joined: Wed Jul 16, 2008 10:16 am

Postby Shibbs » Fri Sep 05, 2008 6:48 pm

Actually i am talking about that java only which Android support.
Android has support for Java API's and the FileBrowser we have in place uses Java File Api's to a large extent.
However by the Java api's present in Android we can't get the size of a directory like sdcard or can we?
If yes, can someone guide me in how to do it?

i hope now the intent is clear.

Thanks,
Shibbs
Shibbs
Freshman
Freshman
 
Posts: 5
Joined: Fri Sep 05, 2008 3:14 pm

Postby Anm » Wed Oct 15, 2008 8:28 am

Love examples like this. Thanks a bunch.

Updated code for 1.0
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package org.anddev.android.filebrowser;
  3.  
  4.  
  5.  
  6. import java.io.File;
  7.  
  8. import java.util.ArrayList;
  9.  
  10. import java.util.Collections;
  11.  
  12. import java.util.List;
  13.  
  14.  
  15.  
  16. import org.anddev.android.filebrowser.iconifiedlist.IconifiedText;
  17.  
  18. import org.anddev.android.filebrowser.iconifiedlist.IconifiedTextListAdapter;
  19.  
  20.  
  21.  
  22. import android.app.AlertDialog;
  23.  
  24. import android.app.ListActivity;
  25.  
  26. import android.content.DialogInterface;
  27.  
  28. import android.content.Intent;
  29.  
  30. import android.content.DialogInterface.OnClickListener;
  31.  
  32. import android.graphics.drawable.Drawable;
  33.  
  34. import android.net.Uri;
  35.  
  36. import android.os.Bundle;
  37.  
  38. import android.view.View;
  39.  
  40. import android.widget.ListView;
  41.  
  42.  
  43.  
  44. public class AndroidFileBrowser extends ListActivity {
  45.  
  46.  
  47.  
  48.         private enum DISPLAYMODE {
  49.  
  50.                 ABSOLUTE, RELATIVE;
  51.  
  52.         }
  53.  
  54.  
  55.  
  56.         private final DISPLAYMODE displayMode = DISPLAYMODE.RELATIVE;
  57.  
  58.         private List<IconifiedText> directoryEntries = new ArrayList<IconifiedText>();
  59.  
  60.         private File currentDirectory = new File( "/" );
  61.  
  62.  
  63.  
  64.         /** Called when the activity is first created. */
  65.  
  66.         @Override
  67.  
  68.         public void onCreate( Bundle icicle ) {
  69.  
  70.                 super.onCreate( icicle );
  71.  
  72.                 browseToRoot();
  73.  
  74.         }
  75.  
  76.  
  77.  
  78.         /**
  79.  
  80.          * This function browses to the root-directory of the file-system.
  81.  
  82.          */
  83.  
  84.         private void browseToRoot() {
  85.  
  86.                 browseTo( new File( "/" ) );
  87.  
  88.         }
  89.  
  90.  
  91.  
  92.         /**
  93.  
  94.          * This function browses up one level according to the field:
  95.  
  96.          * currentDirectory
  97.  
  98.          */
  99.  
  100.         private void upOneLevel() {
  101.  
  102.                 if( this.currentDirectory.getParent() != null )
  103.  
  104.                         this.browseTo( this.currentDirectory.getParentFile() );
  105.  
  106.         }
  107.  
  108.  
  109.  
  110.         private void browseTo( final File aDirectory ) {
  111.  
  112.                 // On relative we display the full path in the title.
  113.  
  114.                 if( this.displayMode == DISPLAYMODE.RELATIVE )
  115.  
  116.                         this.setTitle( aDirectory.getAbsolutePath() + " :: "
  117.  
  118.                                         + getString( R.string.app_name ) );
  119.  
  120.                 if( aDirectory.isDirectory() ) {
  121.  
  122.                         this.currentDirectory = aDirectory;
  123.  
  124.                         fill( aDirectory.listFiles() );
  125.  
  126.                 } else {
  127.  
  128.                         OnClickListener okButtonListener = new OnClickListener() {
  129.  
  130.                                 // @Override
  131.  
  132.                                 public void onClick( DialogInterface arg0, int arg1 ) {
  133.  
  134.                                         // Lets start an intent to View the file, that was
  135.  
  136.                                         // clicked...
  137.  
  138.                                         AndroidFileBrowser.this.openFile( aDirectory );
  139.  
  140.                                 }
  141.  
  142.                         };
  143.  
  144.                         OnClickListener cancelButtonListener = new OnClickListener() {
  145.  
  146.                                 // @Override
  147.  
  148.                                 public void onClick( DialogInterface arg0, int arg1 ) {
  149.  
  150.                                         // Do nothing ^^
  151.  
  152.                                 }
  153.  
  154.                         };
  155.  
  156.                        
  157.  
  158.                         createFileOpenDialog(
  159.  
  160.                                 "Question", R.drawable.folder,
  161.  
  162.                                 "Do you want to open that file?\n" + aDirectory.getName(),
  163.  
  164.                                 "OK", okButtonListener, "Cancel", cancelButtonListener
  165.  
  166.                         ).show();
  167.  
  168.                 }
  169.  
  170.         }
  171.  
  172.  
  173.  
  174.         private AlertDialog createFileOpenDialog(
  175.  
  176.                         String title, int icon_res, String message,
  177.  
  178.                         String positive_text, OnClickListener positive_listener,
  179.  
  180.                         String negative_text, OnClickListener negative_listener
  181.  
  182.                 )
  183.  
  184.         {
  185.  
  186.                 // TODO: Create adb at initialization so we don't have to go through this every time
  187.  
  188.                 AlertDialog.Builder adb = new AlertDialog.Builder( this );
  189.  
  190.                 adb.setTitle( title );
  191.  
  192.                 adb.setIcon( icon_res );
  193.  
  194.                 adb.setMessage( message );
  195.  
  196.                 adb.setPositiveButton( positive_text, positive_listener );
  197.  
  198.                 adb.setNegativeButton( negative_text, negative_listener );
  199.  
  200.  
  201.  
  202.                 return adb.create();
  203.  
  204.         }
  205.  
  206.  
  207.  
  208.         private void openFile( File aFile ) {
  209.  
  210.                 Intent myIntent =
  211.  
  212.                         new Intent(
  213.  
  214.                                 android.content.Intent.ACTION_VIEW,
  215.  
  216.                                 Uri.parse( "file://" + aFile.getAbsolutePath() )
  217.  
  218.                         );
  219.  
  220.                 startActivity( myIntent );
  221.  
  222.         }
  223.  
  224.  
  225.  
  226.         private void fill( File[] files ) {
  227.  
  228.                 this.directoryEntries.clear();
  229.  
  230.  
  231.  
  232.                 // Add the "." == "current directory"
  233.  
  234.                 this.directoryEntries.add( new IconifiedText(
  235.  
  236.                                 getString( R.string.current_dir ), getResources().getDrawable(
  237.  
  238.                                                 R.drawable.folder ) ) );
  239.  
  240.                 // and the ".." == 'Up one level'
  241.  
  242.                 if( this.currentDirectory.getParent() != null )
  243.  
  244.                         this.directoryEntries.add( new IconifiedText(
  245.  
  246.                                         getString( R.string.up_one_level ), getResources()
  247.  
  248.                                                         .getDrawable( R.drawable.uponelevel ) ) );
  249.  
  250.  
  251.  
  252.                 Drawable currentIcon = null;
  253.  
  254.                 for( File currentFile : files ) {
  255.  
  256.                         if( currentFile.isDirectory() ) {
  257.  
  258.                                 currentIcon = getResources().getDrawable( R.drawable.folder );
  259.  
  260.                         } else {
  261.  
  262.                                 String fileName = currentFile.getName();
  263.  
  264.                                 /*
  265.  
  266.                                  * Determine the Icon to be used, depending on the FileEndings
  267.  
  268.                                  * defined in: res/values/fileendings.xml.
  269.  
  270.                                  */
  271.  
  272.                                 if( checkEndsWithInStringArray( fileName, getResources()
  273.  
  274.                                                 .getStringArray( R.array.fileEndingImage ) ) ) {
  275.  
  276.                                         currentIcon = getResources().getDrawable( R.drawable.image );
  277.  
  278.                                 } else if( checkEndsWithInStringArray( fileName, getResources()
  279.  
  280.                                                 .getStringArray( R.array.fileEndingWebText ) ) ) {
  281.  
  282.                                         currentIcon = getResources().getDrawable(
  283.  
  284.                                                         R.drawable.webtext );
  285.  
  286.                                 } else if( checkEndsWithInStringArray( fileName, getResources()
  287.  
  288.                                                 .getStringArray( R.array.fileEndingPackage ) ) ) {
  289.  
  290.                                         currentIcon = getResources()
  291.  
  292.                                                         .getDrawable( R.drawable.packed );
  293.  
  294.                                 } else if( checkEndsWithInStringArray( fileName, getResources()
  295.  
  296.                                                 .getStringArray( R.array.fileEndingAudio ) ) ) {
  297.  
  298.                                         currentIcon = getResources().getDrawable( R.drawable.audio );
  299.  
  300.                                 } else {
  301.  
  302.                                         currentIcon = getResources().getDrawable( R.drawable.text );
  303.  
  304.                                 }
  305.  
  306.                         }
  307.  
  308.                         switch( this.displayMode ) {
  309.  
  310.                                 case ABSOLUTE:
  311.  
  312.                                         /* On absolute Mode, we show the full path */
  313.  
  314.                                         this.directoryEntries.add( new IconifiedText( currentFile
  315.  
  316.                                                         .getPath(), currentIcon ) );
  317.  
  318.                                         break;
  319.  
  320.                                 case RELATIVE:
  321.  
  322.                                         /*
  323.  
  324.                                          * On relative Mode, we have to cut the current-path at the
  325.  
  326.                                          * beginning
  327.  
  328.                                          */
  329.  
  330.                                         int currentPathStringLength = this.currentDirectory
  331.  
  332.                                                         .getAbsolutePath().length();
  333.  
  334.                                         this.directoryEntries.add( new IconifiedText( currentFile
  335.  
  336.                                                         .getAbsolutePath().substring(
  337.  
  338.                                                                         currentPathStringLength ), currentIcon ) );
  339.  
  340.  
  341.  
  342.                                         break;
  343.  
  344.                         }
  345.  
  346.                 }
  347.  
  348.                 Collections.sort( this.directoryEntries );
  349.  
  350.  
  351.  
  352.                 IconifiedTextListAdapter itla = new IconifiedTextListAdapter( this );
  353.  
  354.                 itla.setListItems( this.directoryEntries );
  355.  
  356.                 this.setListAdapter( itla );
  357.  
  358.         }
  359.  
  360.  
  361.  
  362.         @Override
  363.  
  364.         protected void onListItemClick( ListView l, View v, int position, long id ) {
  365.  
  366.                 super.onListItemClick( l, v, position, id );
  367.  
  368.  
  369.  
  370.                 String selectedFileString = this.directoryEntries.get( position )
  371.  
  372.                                 .getText();
  373.  
  374.                 if( selectedFileString.equals( getString( R.string.current_dir ) ) ) {
  375.  
  376.                         // Refresh
  377.  
  378.                         this.browseTo( this.currentDirectory );
  379.  
  380.                 } else if( selectedFileString
  381.  
  382.                                 .equals( getString( R.string.up_one_level ) ) ) {
  383.  
  384.                         this.upOneLevel();
  385.  
  386.                 } else {
  387.  
  388.                         File clickedFile = null;
  389.  
  390.                         switch( this.displayMode ) {
  391.  
  392.                                 case RELATIVE:
  393.  
  394.                                         clickedFile = new File( this.currentDirectory
  395.  
  396.                                                         .getAbsolutePath()
  397.  
  398.                                                         + this.directoryEntries.get( position ).getText() );
  399.  
  400.                                         break;
  401.  
  402.                                 case ABSOLUTE:
  403.  
  404.                                         clickedFile = new File( this.directoryEntries
  405.  
  406.                                                         .get( position ).getText() );
  407.  
  408.                                         break;
  409.  
  410.                         }
  411.  
  412.                         if( clickedFile != null )
  413.  
  414.                                 this.browseTo( clickedFile );
  415.  
  416.                 }
  417.  
  418.         }
  419.  
  420.  
  421.  
  422.         /**
  423.  
  424.          * Checks whether checkItsEnd ends with one of the Strings from fileEndings
  425.  
  426.          */
  427.  
  428.         private boolean checkEndsWithInStringArray( String checkItsEnd,
  429.  
  430.                         String[] fileEndings ) {
  431.  
  432.                 for( String aEnd : fileEndings ) {
  433.  
  434.                         if( checkItsEnd.endsWith( aEnd ) )
  435.  
  436.                                 return true;
  437.  
  438.                 }
  439.  
  440.                 return false;
  441.  
  442.         }
  443.  
  444. }
  445.  
  446.  
Parsed in 0.071 seconds, using GeSHi 1.0.8.4
Anm
Freshman
Freshman
 
Posts: 7
Joined: Wed Oct 15, 2008 8:21 am

.apk for Android 1.0 (G1)

Postby fabricapo » Thu Oct 23, 2008 5:17 am

Any chance you can post an .apk for the G1 somewhere? It is a much needed apps...

fabrizio
fabricapo
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Oct 23, 2008 5:14 am

Postby miserlou » Wed Oct 29, 2008 12:26 am

Daaamn, this is handy! Came here looking for a good file open dialog, this is wonderful. Just what I need.

I'm going to be releasing my app under the GNU-GPL, can I consider your code to be public domain?
User avatar
miserlou
Freshman
Freshman
 
Posts: 7
Joined: Wed Oct 29, 2008 12:22 am

Top

Postby ravi » Sun Nov 16, 2008 10:13 pm

Hi Nicholas,

I'm trying to install the apk into /system/app using adb, but am unable to do so (getting a 'read-only file system' message). Are there any specific sequence of steps to install the apk in the above folder for administrative privileges? I'm a beginner and this may be a basic question (thank you for your patience).

Thanks,
Ravi


plusminus wrote:Hello cabernet1976,

the reason you cannot browse to that folder is because you need Administrator-Privileges, which you do not have with your app.

I've heard, that if you install the apk into /system/app then it will run at a higher level.

Regards,
plusminus
ravi
Freshman
Freshman
 
Posts: 8
Joined: Tue Nov 04, 2008 11:27 pm

Postby Peli » Thu Dec 11, 2008 2:30 pm

Hi PlusMinus,

I wanted to let you know that we built the OI File Manager:
http://www.openintents.org/en/node/159

It is loosely based on your :arrow: AndDev File Browser :P , but we modified navigation through buttons at the top of the screen, and the "back" key that works whenever a folder had been selected from the list.

Also, we do not hard-code the images, but take those from the applications that can handle the corresponding MIME types. What we need is a mapping between extensions (.mp3) to MIME types (audio/mp3), which we have defined in an xml file.

But the best part is that we included new intents :D : You can use simple intents in your application to call it for "Open" :) ,
"Save" :shock: , and "Select folder" :lol: . Through intent extras, the new activity title and button text can actually be set to anything the calling application likes.

In the simplest version, just use:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent intent = new Intent("org.openintents.action.PICK_FILE");
  2. startActivityForResult(intent, 1);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


and retrieve the result from getData() in onActivityResult().

A sample application that showcases this in a clean way can be found in the Open Intents repository 8) :
http://code.google.com/p/openintents/so ... ileManager

Here is the main java file:
http://code.google.com/p/openintents/so ... nager.java

You can download OI File Manager here:
http://www.openintents.org/en/download

and if you are interested in how we did it, the source code is available here:
http://code.google.com/p/openintents/downloads/list

:!: Since we plan to improve the OI File Manager in the future, it is best to use the intents from your application :wink: .

Peli
Attachments
filemanager-1-0-0-a.png
OI File Manager
filemanager-1-0-0-a.png (17.23 KiB) Viewed 11835 times
Peli
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Dec 27, 2007 10:54 am

Postby MrSnowflake » Thu Dec 11, 2008 2:44 pm

Oeh, the open and save dialogs look awsome to me!
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby hit1983 » Thu Dec 18, 2008 9:09 am

does this app compatible with the latest 1.0 sdk ? Do you have a plan to update this app using ExpandableListActivity?
hit1983
Freshman
Freshman
 
Posts: 9
Joined: Mon Oct 06, 2008 8:08 am
Location: BeiJing

Postby songotho » Wed Apr 22, 2009 11:33 am

Hi,

Great tutorial, so I can't run the app, Android have errors:
1. in AndroidFileBrowser.java
Code: Select all
import android.net.ContentURI;

2. in DirNameInputActivity.java
Code: Select all
DirNameInputActivity.this.setResult(SUCCESS_RETURN_CODE, edit_returvalue.getText().toString());


Please tell me this error!

Thanks,

--Alex
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

open file

Postby rahulp » Thu Apr 30, 2009 9:51 am

hi when i try to open a file it the app force close dialog appears..
can anybody help me out
rahul phadnis
rahulp
Developer
Developer
 
Posts: 30
Joined: Thu Feb 19, 2009 12:52 pm

open file

Postby rahulp » Thu Apr 30, 2009 1:14 pm

hi when i try to open a file it the app force close dialog appears..
can anybody help me out
rahul phadnis
rahulp
Developer
Developer
 
Posts: 30
Joined: Thu Feb 19, 2009 12:52 pm

Postby songotho » Thu Apr 30, 2009 2:04 pm

I have 2 errors:

this.setListAdapter( itla );

protected void onListItemClick( ListView l, View v, int position, long id ) {
super.onListItemClick( l, v, position, id );

I cant run it.

Thanks,
--Alex
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Postby Cecco » Mon May 11, 2009 5:44 pm

plusminus wrote:Hello ice2age,

yes I'm also a bit surprised about the differences between the output of the program and what the DDMS-View shows...

You probably cannot browse the "/data/"-Folder, because the rights of the Application are not high enough to do so.

I'm not aware of any default-folders for images/music yet. (also not that familiar with Linux)

Regards,
plusminus


Hi,
I'm a italian student, sorry for my english.
I need to read(only read, not write) the /data directory , somebody help me?
Cecco
Once Poster
Once Poster
 
Posts: 1
Joined: Mon May 11, 2009 5:41 pm

Top
PreviousNext

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests