Custom Search

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





























0 comments:

Post a Comment