In Android development we always look for more simple and user-friendly user inputs. We have many custm keyboards, material design ad various other ways. Similarly, when we want user to choose an image from the gallery that’ll be displayed by the application we have various intents and actions such as ACTION_PICK. But, in case of the file formats that may not be an image, we cannot open gallery as only images are the part of the gallery application.
In such situation we invoke a single interface from which the user is able to select images across all his apps (like Gallery, Photos, ES File Explorer, etc.) and folders (Google Drive, Recent, Downloads, etc.) using Intents. But, this will take user away from your application as well as there will be 3rd party application dependence. So, we need to create our own custom file browser that will access ur sd-card files and folders.
Refer the below link for complete sample code:-
Download Sample Code
Have a look on few code snippets,
//FileBrowserActivity.java
//activity_filebrowser.xml
//row_item_filebrowser.xml
In such situation we invoke a single interface from which the user is able to select images across all his apps (like Gallery, Photos, ES File Explorer, etc.) and folders (Google Drive, Recent, Downloads, etc.) using Intents. But, this will take user away from your application as well as there will be 3rd party application dependence. So, we need to create our own custom file browser that will access ur sd-card files and folders.
Refer the below link for complete sample code:-
Download Sample Code
Have a look on few code snippets,
//FileBrowserActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | package com.example.harshalbenake.filebrowser; 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.os.Environment; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class FileBrowserActivity 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.activity_filebrowser); 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.row_item_filebrowser, 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.ic_launcher) .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.ic_launcher) .setTitle("Select") .setMessage("Select " + file.getName() + "to server ?") .setPositiveButton("Select", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText( FileBrowserActivity.this, "" + file.getAbsolutePath() + " iss selected ", Toast.LENGTH_SHORT) .show(); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).show(); } } public class Option implements Comparable<Option>{ private String name; private String data; private String path; public Option(String n,String d,String p) { name = n; data = d; path = p; } public String getName() { return name; } public String getData() { return data; } public String getPath() { return path; } @Override public int compareTo(Option o) { if(this.name != null) return this.name.toLowerCase().compareTo(o.getName().toLowerCase()); else throw new IllegalArgumentException(); } } } |
//activity_filebrowser.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <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> |
//row_item_filebrowser.xml
1 2 3 4 5 6 7 8 | <?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" /> |
No comments:
Post a Comment