Showing posts with label File System. Show all posts
Showing posts with label File System. Show all posts

Saturday, April 14, 2012

Zipping File and Folder in android


While we are attaching any folder/file to mail or other attachment so we really need to attach folder with more than one sub folder. So we need to compress it so called zipping . It necessary in Mobile OS as in Windows.

So for this android provide ZipOutPutStream  and ZipEntry. ZipOutPutStream read complete folder and then ZipEntry zip all the file inside folder to a new compress folder.

Zipping File is very easy.If you want dynamically select which file/folder to zipped then see this


import android.util.Log; 
import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipOutputStream; 
 
 
public class Compress { 
  private static final int BUFFER = 2048
 
  private String[] _files; 
  private String _zipFile; 
 
  public Compress(String[] files, String zipFile) { 
    _files = files; 
    _zipFile = zipFile; 
  } 
 
  public void zip() { 
    try  { 
      BufferedInputStream origin = null
      FileOutputStream dest = new FileOutputStream(_zipFile); 
 
      ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); 
 
      byte data[] = new byte[BUFFER]; 
 
      for(int i=0; i < _files.length; i++) { 
        Log.v("Compress""Adding: " + _files[i]); 
        FileInputStream fi = new FileInputStream(_files[i]); 
        origin = new BufferedInputStream(fi, BUFFER); 
        ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1)); 
        out.putNextEntry(entry); 
        int count; 
        while ((count = origin.read(data, 0, BUFFER)) != -1) { 
          out.write(data, 0, count); 
        } 
        origin.close(); 
      } 
 
      out.close(); 
    } catch(Exception e) { 
      e.printStackTrace(); 
    } 
 
  } 
 
In constructor we have two string parameter.first pass array of file inside a folder .Then file path to which zipped folder will save if it is not present then this code will create new zip folder.

Friday, April 13, 2012

Android File explorer ,pick image, video, Audio from your phone drive

In android we know sdcard is main drive to store information. Sometimes we need to read/write file from sdcard. In this case we can give static path and easily can read/write using FileInputStream and FileOutputStream. But in most case we need to select it  dynamically on user demand.After selecting it we can modify it, we can share or we can send it to anywhere


So today I am going to make a simple application like File-explorer. It will help in my next Article Ziping and Unziping Folder in android.


Step1) Create one project File-Explorer 

Step 2) Change your activity to following code





package com.AndroidExplorer;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class AndroidExplorer extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root = "/sdcard";
private TextView myPath;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.explorer);
myPath = (TextView) findViewById(R.id.path);
getDir(root);
}
private void getDir(String dirPath) {
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if (!dirPath.equals(root)) {
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for (int i = 0; i < files.length; i++) {
File file = files[i];
path.add(file.getPath());
if (file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this,
R.layout.explorer_row, item);
setListAdapter(fileList);
         }
       File file;
       @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
file = new File(path.get(position));
if (file.isDirectory()) {
  if (file.canRead())
getDir(path.get(position));
else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName()+ "] folder can't be read!")
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
   @Override public void onClick(DialogInterface dialog,int which) {
 }}).show();
}
} else {
    new AlertDialog.Builder(this)
   .setIcon(R.drawable.icon)
   .setTitle("Select")
   setMessage("Select " + file.getName() + "to server ?")
  .setPositiveButton("Select",new DialogInterface.OnClickListener() {
         @Override public void onClick(DialogInterface dialog,int which) {
Toast.makeText(
                     AndroidExplorer.this,"" + file.getAbsolutePath()+ " iss selected ",300)
.show();
    }
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
          public void onClick(DialogInterface dialog,int which) {
dialog.dismiss();
}
}).show();
}
    }
      } 
Step 3 Now our coding part has been done.We will create two xml. As i have take one List-Activtiy   so it need two layout one main.xml and another one to inflating into ListView row.Click me for advance ListView


explorer.xml inside res/layout folder


 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
android:id="@+id/path"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:cacheColorHint="#B26B00"
    android:fadingEdge="none"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="No Data"
/>
</LinearLayout>


explorer_row in same folder



<?xml version="1.0" encoding="utf-8"?>
<TextView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowtext" android:padding="10dp"
android:background="#C0C0C0" android:textColor="#000"
android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="23sp" />


Now enjoy you can download this complete project from this link.

                                      Download this Project


These are the screen shot of that project