Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ant.h2h.feelsafe;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FilenameFilter;
- import java.io.InputStream;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.ListAdapter;
- import android.widget.TextView;
- import android.widget.Toast;
- public class FileExploreActivity extends Activity {
- // Stores names of traversed directories
- ArrayList<String> str = new ArrayList<String>();
- // Check if the first level of the directory structure is the one showing
- private Boolean firstLvl = true;
- private static final String TAG = "F_PATH";
- private Item[] fileList;
- private File path = new File(Environment.getExternalStorageDirectory() + "");
- private String chosenFile;
- private static final int DIALOG_LOAD_FILE = 1000;
- ListAdapter adapter;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- loadFileList();
- showDialog(DIALOG_LOAD_FILE);
- Log.d(TAG, path.getAbsolutePath());
- }
- private void loadFileList() {
- try {
- path.mkdirs();
- } catch (SecurityException e) {
- Log.e(TAG, "unable to write on the sd card ");
- }
- // Checks whether path exists
- if (path.exists()) {
- FilenameFilter filter = new FilenameFilter() {
- @Override
- public boolean accept(File dir, String filename) {
- File sel = new File(dir, filename);
- // Filters based on whether the file is hidden or not
- return (sel.isFile() || sel.isDirectory())
- && !sel.isHidden();
- }
- };
- String[] fList = path.list(filter);
- fileList = new Item[fList.length];
- for (int i = 0; i < fList.length; i++) {
- fileList[i] = new Item(fList[i], getResources().getDrawable(R.drawable.file_icon));
- // Convert into file path
- File sel = new File(path, fList[i]);
- // Set drawables
- if (sel.isDirectory()) {
- fileList[i].icon = getResources().getDrawable(R.drawable.directory_icon);
- Log.d("DIRECTORY", fileList[i].file);
- } else {
- //get file extension
- String ext = getFileExtension(fileList[i].file);
- if((ext.equals("jpg")||ext.equals("png"))){
- String image = path+"/"+fileList[i].file;
- InputStream is;
- try {
- is = new FileInputStream(image);
- BitmapFactory.Options options=new BitmapFactory.Options();
- options.inSampleSize = 8;
- Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);
- fileList[i].icon = new BitmapDrawable(getResources(),preview_bitmap);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- Log.d("FILE", fileList[i].file);
- }
- }
- if (!firstLvl) {
- Item temp[] = new Item[fileList.length + 1];
- for (int i = 0; i < fileList.length; i++) {
- temp[i + 1] = fileList[i];
- }
- temp[0] = new Item("Up", getResources().getDrawable(R.drawable.directory_up));
- fileList = temp;
- }
- } else {
- Log.e(TAG, "path does not exist");
- }
- adapter = new ArrayAdapter<Item>(this,
- android.R.layout.select_dialog_item, android.R.id.text1,
- fileList) {
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // creates view
- View view = super.getView(position, convertView, parent);
- TextView textView = (TextView) view
- .findViewById(android.R.id.text1);
- // put the image on the text view
- textView.setCompoundDrawablesWithIntrinsicBounds(
- fileList[position].icon, null, null, null);
- // add margin between image and text (support various screen
- // densities)
- int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
- textView.setCompoundDrawablePadding(dp5);
- return view;
- }
- };
- }
- private class Item {
- public String file;
- public Drawable icon;
- public Item(String file, Drawable drawable) {
- this.file = file;
- this.icon = drawable;
- }
- @Override
- public String toString() {
- return file;
- }
- }
- @Override
- protected Dialog onCreateDialog(int id) {
- Dialog dialog = null;
- final AlertDialog.Builder builder = new AlertDialog.Builder(this);
- if (fileList == null) {
- Log.e(TAG, "No files loaded");
- dialog = builder.create();
- return dialog;
- }
- switch (id) {
- case DIALOG_LOAD_FILE:
- builder.setTitle("Choose your file");
- builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- chosenFile = fileList[which].file;
- File sel = new File(path + "/" + chosenFile);
- if (sel.isDirectory()) {
- firstLvl = false;
- // Adds chosen directory to list
- str.add(chosenFile);
- fileList = null;
- path = new File(sel + "");
- loadFileList();
- //builder.setMessage(DIALOG_LOAD_FILE);
- removeDialog(DIALOG_LOAD_FILE);
- showDialog(DIALOG_LOAD_FILE);
- Log.d(TAG, path.getAbsolutePath());
- }
- // Checks if 'up' was clicked
- else if (chosenFile.equalsIgnoreCase("up") && !sel.exists()) {
- // present directory removed from list
- String s = str.remove(str.size() - 1);
- // path modified to exclude present directory
- path = new File(path.toString().substring(0,
- path.toString().lastIndexOf(s)));
- fileList = null;
- // if there are no more directories in the list, then
- // its the first level
- if (str.isEmpty()) {
- firstLvl = true;
- }
- loadFileList();
- //builder.setMessage(DIALOG_LOAD_FILE);
- removeDialog(DIALOG_LOAD_FILE);
- showDialog(DIALOG_LOAD_FILE);
- Log.d(TAG, path.getAbsolutePath());
- }
- // File picked
- else {
- // Perform action with file picked
- if(getFileExtension(chosenFile).equals("jpg")||getFileExtension(chosenFile).equals("png")){
- Log.d("FILE SEND", path+"/"+chosenFile);
- Intent i=new Intent();
- i.setAction("file");
- i.putExtra("file",path+"/"+chosenFile);
- sendBroadcast(i);
- }
- else{
- Context context = getApplicationContext();
- CharSequence text = getResources().getString(R.string.follow_register_wrong_img_format);
- int duration = Toast.LENGTH_SHORT;
- Toast toast = Toast.makeText(context, text, duration);
- toast.show();
- }
- finish();
- }
- }
- });
- break;
- }
- dialog = builder.show();
- return dialog;
- }
- public String getFileExtension(String fileName){
- String extension = "";
- int i = fileName.lastIndexOf('.');
- int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));
- if (i > p) {
- extension = fileName.substring(i+1);
- }
- return extension;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement