Thursday, 8 May 2014

ANdroid read the file from Folder(SD Card)

String unzipLocation = Environment.getExternalStorageDirectory() + "/Agile/unzipped/";        
        String filePthe=Environment.getExternalStorageDirectory()+"/Agile/unzipped/agile/";
       
        /*try {
            Log.d("-------------", "started");
            Decompress d = new Decompress(zipFile, unzipLocation);   
            Log.d("-------------", "zip success");
            d.unzip();
        } catch (Exception e) {
            Log.d("-------------", "zip fail");
        }*/
       
        File file=new File(filePthe);
       
        try {
            download=new Download();
            //download.getClass();
            //download.getListFiles(file);
            Log.d("---name----", ""+file.getName());
            Log.d("----parent---", ""+file.getParent());
            Log.d("-------", ""+file.getPath());
            Log.d("----array value---", ""+file.listFiles().toString());
            Log.d("----array length---", ""+file.listFiles().length);
           
            File[] files = file.listFiles();
            for(int i=0;i<file.listFiles().length;i++){
                Log.d("---name----", ""+files[i].getName());
            }
           
           
            //Log.d("------folder size-----", ""+download.getListFiles(file).size());
        } catch (Exception e) {
        //    Log.d("-------problem", e.getMessage());
            e.printStackTrace();
        }

====================================================================

download the file from the URL

public void downloadFile(String url) {
       
        try {           
        String imageFile = url.substring(url.lastIndexOf("/") + 1, url.length());
              URL u = new URL(url);
              URLConnection conn = u.openConnection();
              conn.setRequestProperty("Authorization","Basic "+ Base64.encodeToString(( "demo-tab1:e$y#2012" )
        .getBytes(), Base64.NO_WRAP));
              int contentLength = conn.getContentLength();
              DataInputStream stream = new DataInputStream(u.openStream());
              byte[] buffer = new byte[contentLength];
              stream.readFully(buffer);
              stream.close();
                DataOutputStream fos = new DataOutputStream(new FileOutputStream(Environment.getExternalStorageDirectory()+"/Agile" + "/"+imageFile));
                fos.write(buffer);
                fos.flush();
                fos.close();

          } catch(FileNotFoundException e) {
              return;
          } catch (IOException e) {
              return; // swallow a 404
          }       
       
        }

===========================================================================
this is class for unzipp folder

public class Decompress {

    private String _zipFile;
      private String _location;
   
      public Decompress(String zipFile, String location) {
        _zipFile = zipFile;
        _location = location;
   
        _dirChecker("");
      }
   
      public void unzip() {
        try  {
          FileInputStream fin = new FileInputStream(_zipFile);
          ZipInputStream zin = new ZipInputStream(fin);
          ZipEntry ze = null;
          while ((ze = zin.getNextEntry()) != null) {
            Log.v("Decompress", "Unzipping " + ze.getName());
   
            if(ze.isDirectory()) {
              _dirChecker(ze.getName());
            } else {
              FileOutputStream fout = new FileOutputStream(_location + ze.getName());
              for (int c = zin.read(); c != -1; c = zin.read()) {
                fout.write(c);
              }
   
              zin.closeEntry();
              fout.close();
            }
            
          }
          zin.close();
        } catch(Exception e) {
          Log.e("Decompress", "unzip", e);
        }
   
      }
   
      private void _dirChecker(String dir) {
        File f = new File(_location + dir);
   
        if(!f.isDirectory()) {
          f.mkdirs();
        }
      }
    }


Call Decompress class like this:

String zipFile = Environment.getExternalStorageDirectory() + "/files.zip"; 
String unzipLocation = Environment.getExternalStorageDirectory() + "/unzipped/"; 
 
Decompress d = new Decompress(zipFile, unzipLocation); 
d.unzip(); 


No comments:

Post a Comment