- These days in order to scan all the pictures of SCard card is very tangled, I originally thought this is a very simple thing, but I found that I was wrong. There is no complete code on the web, just bits and pieces of code that can scan a single file. After today's code debugging passed, I think I need to share with you.
1, because the mobile phone side of the scan, so it is best to open a thread. It is very important to have the scan done in a sub-thread, so start looking at this class now! This code can scan out all the files on the SCard card.
- Public class Getfilepaththread implements Runnable {
- Public String pathstring;
- Public List < String > listpath;
- Public Getfilepaththread(string path,List<string> List ) {
- This . pathstring=path;
- This . Listpath=list;
- }
- Public void Run (){
- File file = New File (pathstring) ;
- if (file. isdirectory ()){
- file fs[]=file. Listfiles ();
- if (fs!=null) {
- System . Out. println ("The number of files contained in the directory--->"+fs. length );
- if (fs. length >0){
- for ( int i= 0 I<, fs length I+ + ) Span style= "COLOR: #0000CC" >{
- if ( fs[ i] isfile ( ) {
- listpath add ( fs[ i] getabsolutepath ( )
- system Out println ( "file---->" + fs[ i getabsolutepath ( )
- } else if ( fs< Span style= "COLOR: #0000CC" >[ i] . isdirectory ( ) {
- system Out println ( "directory---->" + fs[ i getabsolutepath ( ) + "/" )
- New Thread(NewGetfilepaththread(Fs[I].GetAbsolutePath()+"/",Listpath)).Start();
- //GetFilePath (Fs[i].getabsolutepath (), listpath);
- }
- }
- }
- }
- }
- }
- }
2. Scan the image files we need , of course, before you define it.
Private List list=new ArrayList ();
- StringBuffer Buffer = New StringBuffer ();
- for ( int I= 0; I<, list . size ( ; I+ + ) Span style= "COLOR: #0000CC" >{
- if ( list get ( i endswith ( ". jpg" ) {
- Buffer . Append (list. Get (i) . toString ()+"n");
- }
- }
3, remember to join about SCard card Read and Write permission, this is very important. As for the thread, I won't say much.
Summary: This code in small place tangled for a long time, really should not, the main problem is two, one is how to traverse the folder sub-file, the second, how to get the file path added to;
Hope these can be useful to everyone!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Scan SCard Card all pictures