Android

Android Projects : Manage Files Extension
Author : Ishtek

Using Android API Level : 7 
In this page I am going to show, how I have created a project in order to quikly
change files extension in any folder that is selected.

I was inspired by the fact that I had to manually change many files extension from
one type to another and vice versa, and doing it manually would take much time.

Instead I thought of creating this application with very simple objective, allow
user to select a folder, select a from or existing extension to another extension.

Logic for this application on Android Platform is very simple, just take the
folder as user input, check whether this exists, if yes, find all the files
under this folder, and iterator through the list of files, and just apply 
renameTo method from the file object. 


I won't recommend this application to be used as it is by anyone without proper backup
of all the files, or one may have the risk of losing all the data/files used.


Some of the screen shots of this application are as follows:
This application would be having files for layout, menu, images, activity Java files. activity_manage_files.xml
<?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="wrap_content" android:orientation="vertical" > <SurfaceView android:layout_width="fill_parent" android:layout_height="50dp" android:background="#7766ff" /> <SurfaceView android:layout_width="fill_parent" android:layout_height="20dp" android:background="#FFFFff" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="140dp" android:layout_height="wrap_content" android:text="@string/lblPath" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textColor="#000000"/> <Spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:entries="@array/spinner1text" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="140dp" android:layout_height="wrap_content" android:text="@string/lblExt1" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textColor="#000000"/> <Spinner android:id="@+id/spinner2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:entries="@array/spinner2text"/> </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text=""/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="140dp" android:layout_height="wrap_content" android:text="@string/lblExt2" android:textColor="#000000"/> <Spinner android:id="@+id/spinner3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:entries="@array/spinner3text" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btChange" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/lblBtConvert" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <Button android:id="@+id/btClear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/lblBtClear" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:id="@+id/txt1" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/txt2" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/txt3" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> </LinearLayout>
values.xml
<resources> <string-array name="spinner1text"> <item>/sdcard/java/.jp/</item> <item>/sdcard/java/.fr/</item> <item>/sdcard/java/.fr/</item> <item>/sdcard/java/.bk/</item> <item>/sdcard/java/.be/</item> <item>/sdcard/java/.me/</item> </string-array> <string-array name="spinner2text"> <item>.jpg</item> <item>.java</item> <item>.mp4</item> <item>.png</item> <item>.flv</item> </string-array> <string-array name="spinner3text"> <item>.jpg</item> <item>.java</item> <item>.mp4</item> <item>.png</item> <item>.flv</item> </string-array> </resources>
These values are to be used for populating various drop-downs/spinners on screen. strings.xml
<resources> <string name="app_name">ManageFiles</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="dir">DIR</string> <string name="toExt">To Extension</string> <string name="fromExt">From Extension</string> <string name="title_activity_manage_files">Manage Files</string> <string name="lblPath">Select Path/Folder :</string> <string name="lblExt1">From File Extension :</string> <string name="lblExt2">To File Extension :</string> <string name="lblBtConvert">Convert</string> <string name="lblBtClear">Clear</string> </resources>
These strings are used to be used with the layout/screen. activity_manage_files.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/dirChng" android:title="@string/dir" android:orderInCategory="100" android:icon="@drawable/folder"/> <item android:id="@+id/toExtChng" android:title="@string/toExt" android:orderInCategory="100" android:icon="@drawable/fromext" /> <item android:id="@+id/fromExtChng" android:title="@string/fromExt" android:orderInCategory="100" android:icon="@drawable/toext"/> </menu>
These are the menus to be shown when context menu is activated. The main activity Java file is written by using Android API level 7 : ManageFiles.java
/** * This file is provided on "AS IS" basis. * Author: Ishtek * Date: 20-Oct-2013 */ import java.io.File; import java.io.FilenameFilter; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.view.Menu; import android.view.SurfaceView; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; public class ManageFiles extends Activity { private SharedPreferences sp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_manage_files); final Spinner edtText1 = (Spinner) findViewById(R.id.spinner1); final Spinner edtExt1 = (Spinner) findViewById(R.id.spinner2); final Spinner edtExtTo = (Spinner) findViewById(R.id.spinner3); sp = getSharedPreferences("str_values", getApplicationContext().MODE_WORLD_WRITEABLE); final Button btConvert = (Button) findViewById(R.id.btChange); Button btClear = (Button) findViewById(R.id.btClear); File file = new File(edtText1.getSelectedItem().toString()); if(!file.isDirectory()) { btConvert.setEnabled(false); file = null; } edtText1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { File file = new File(arg0.getSelectedItem().toString()); if(file.isDirectory()) { btConvert.setEnabled(true); } if(!file.isDirectory()) { btConvert.setEnabled(false); } file = null; } @Override public void onNothingSelected(AdapterView<?> arg0) { File file = new File(arg0.getSelectedItem().toString()); if(file.isDirectory()) { btConvert.setEnabled(true); } if(!file.isDirectory()) { btConvert.setEnabled(false); } file = null; } }); btClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { } }); btConvert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { try { File file = new File(edtText1.getSelectedItem().toString()); if(file.isDirectory()) { File[] files = file.listFiles(new FilenameFilter() { @Override public boolean accept(File arg0, String arg1) { if(arg1.toLowerCase().endsWith(edtExt1.getSelectedItem().toString())) { return true; } else { return false; } } }); for(int i=0;i<files.length;i++) { files[i].renameTo(new File(edtText1.getSelectedItem().toString()+files[i] .getName().substring(0, files[i].getName() .indexOf(".")) +edtExtTo.getSelectedItem().toString())); } Toast.makeText(getApplicationContext(), "Done..."+files .length, Toast.LENGTH_LONG).show(); SharedPreferences.Editor editor = sp.edit(); editor.putString("dir1", edtText1.getSelectedItem().toString()); editor.commit(); } else { Toast.makeText(getApplicationContext(),edtText1.getSelectedItem().toString() +" not a Directory", Toast.LENGTH_SHORT).show(); } } catch (Exception ex) { Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_manage_files, menu); return true; } }
There are three icons used to decorate menu items are as follows:          As of now, this application assumes that there are existing folders in external directory and fo these are hard coded in the values.xml file, and menu items are non-functional. More features are to be developed, such as providing inputs for the folders to be choosen and saved in shared preferences along with extensions for the from and to extensions, and to be auto populated in the spinner for the user to choose those rather than typing folder, extensions etc. Recently I have added some more features to this existing example, such as provision for renaming bulk files with autogenerated file names automatically by using an initial alphabet with incremental numbers/digits appended to it. If anyone interested in trying out this example, I would say, please take proper care while testing/using this application such as taking appropriate backups for your files etc., you might lose any document if any unforseen bug is managed to find a place in this example. As thi application is dealing with naming/renaming/overwriting existing file(s).
Android-Projects-Manage-Files-Extension/.classpath

<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src"/> <classpathentry kind="src" path="gen"/> <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/> <classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/> <classpathentry kind="output" path="bin/classes"/> </classpath>


Android-Projects-Manage-Files-Extension/.project

<?xml version="1.0" encoding="UTF-8"?> <projectDescription> <name>ManageFiles</name> <comment></comment> <projects> </projects> <buildSpec> <buildCommand> <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>com.android.ide.eclipse.adt.ApkBuilder</name> <arguments> </arguments> </buildCommand> </buildSpec> <natures> <nature>com.android.ide.eclipse.adt.AndroidNature</nature> <nature>org.eclipse.jdt.core.javanature</nature> </natures> </projectDescription>


Android-Projects-Manage-Files-Extension/AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.techienjoy" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".ManageFiles" android:label="@string/title_activity_manage_files" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".DirActivity"></activity> </application> </manifest>


Android-Projects-Manage-Files-Extension/project.properties

# This file is automatically generated by Android Tools. # Do not modify this file -- YOUR CHANGES WILL BE ERASED! # # This file must be checked in Version Control Systems. # # To customize properties used by the Ant build system edit # "ant.properties", and override values to adapt the script to your # project structure. # # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt # Project target. target=android-7


Android-Projects-Manage-Files-Extension/res/drawable/exit.png



Android-Projects-Manage-Files-Extension/res/drawable/folder.png



Android-Projects-Manage-Files-Extension/res/drawable/fromext.png



Android-Projects-Manage-Files-Extension/res/drawable/toext.png



Android-Projects-Manage-Files-Extension/res/layout/activity_manage_files.xml

<?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="wrap_content" android:orientation="vertical" > <SurfaceView android:layout_width="fill_parent" android:layout_height="50dp" android:background="#7766ff" /> <SurfaceView android:layout_width="fill_parent" android:layout_height="20dp" android:background="#FFFFff" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="140dp" android:layout_height="wrap_content" android:text="@string/lblPath" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textColor="#000000"/> <Spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:entries="@array/spinner1text" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="140dp" android:layout_height="wrap_content" android:text="@string/lblExt1" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textColor="#000000"/> <Spinner android:id="@+id/spinner2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:entries="@array/spinner2text"/> </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text=""/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="140dp" android:layout_height="wrap_content" android:text="@string/lblExt2" android:textColor="#000000"/> <Spinner android:id="@+id/spinner3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:entries="@array/spinner3text" /> </LinearLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/txtMsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Select below check box to enter file name initializer:"/> <CheckBox android:id="@+id/enableDisable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txtMsg"/> <EditText android:id="@+id/fileNameInit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txtMsg" android:layout_toRightOf="@+id/enableDisable" android:layout_centerVertical="true" android:width="150dp" android:enabled="false"/> <Button android:id="@+id/btValidate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txtMsg" android:text="Validate" android:layout_toRightOf="@+id/fileNameInit" android:layout_centerVertical="true" android:width="150dp" android:enabled="false"/> </RelativeLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btChange" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/lblBtConvert" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <Button android:id="@+id/btClear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/lblBtClear" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:id="@+id/txt1" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/txt2" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/txt3" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="18dp" android:textColor="#77aa66" android:text=" Please select/use Menu for adding folders !!!"/> </LinearLayout>


Android-Projects-Manage-Files-Extension/res/layout/dir_list_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/txtMenuDir" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="#000000" android:textSize="24dp" android:textStyle="bold" android:layout_margin="10dp" android:padding="10dp"/>


Android-Projects-Manage-Files-Extension/res/layout/dir_menu_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <SurfaceView android:layout_width="fill_parent" android:layout_height="50dp" android:background="#7766ff"/> <SurfaceView android:layout_width="fill_parent" android:layout_height="20dp" android:background="#FFFFff"/> <TextView android:id="@+id/txtMenuDir" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Folder/Path:"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/sdcard/" android:textColor="#000000" android:textSize="24dp" android:textStyle="bold"/> <EditText android:id="@+id/menuEdtTxt" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_toRightOf="@+id/txtMenuDir"/> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/butnDir" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Add " android:layout_below="@+id/menuEdtTxt"/> <Button android:id="@+id/butnBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Back " android:layout_below="@+id/menuEdtTxt"/> </LinearLayout> <TextView android:id="@+id/lstTxt" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ListView> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Please Long-Press List Item To delete it"/> </LinearLayout>


Android-Projects-Manage-Files-Extension/res/menu/activity_manage_files.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/dirChng" android:title="@string/dir" android:orderInCategory="100" android:icon="@drawable/folder"/> <item android:id="@+id/toExtChng" android:title="@string/toExt" android:orderInCategory="100" android:icon="@drawable/fromext" /> <item android:id="@+id/fromExtChng" android:title="@string/fromExt" android:orderInCategory="100" android:icon="@drawable/toext"/> <item android:id="@+id/exit" android:title="@string/exit" android:orderInCategory="100" android:icon="@drawable/exit"/> </menu>


Android-Projects-Manage-Files-Extension/res/menu/dir_menu_items.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/exit" android:title="@string/exit" android:orderInCategory="100" android:icon="@drawable/exit"/> </menu>


Android-Projects-Manage-Files-Extension/res/values/strings.xml

<resources> <string name="app_name">ManageFiles</string> <string name="menu_settings">Settings</string> <string name="dir">DIR</string> <string name="toExt">To Extension</string> <string name="fromExt">From Extension</string> <string name="exit">Exit</string> <string name="title_activity_manage_files">Manage Files</string> <string name="lblPath">Select Path/Folder :</string> <string name="lblExt1">From File Extension :</string> <string name="lblExt2">To File Extension :</string> <string name="lblBtConvert">Rename with new extension</string> <string name="lblBtClear">Clear</string> </resources>


Android-Projects-Manage-Files-Extension/res/values/styles.xml

<resources> <style name="AppTheme" parent="android:Theme.Light" /> </resources>


Android-Projects-Manage-Files-Extension/res/values/values.xml

<resources> <string-array name="spinner1text"> <item></item> </string-array> <string-array name="spinner2text"> <item>.jpg</item> <item>.java</item> <item>.mp4</item> <item>.png</item> <item>.flv</item> </string-array> <string-array name="spinner3text"> <item>.jpg</item> <item>.java</item> <item>.mp4</item> <item>.png</item> <item>.flv</item> </string-array> </resources>


Android-Projects-Manage-Files-Extension/src/com/techienjoy/DirActivity.java

/** * @author Ishteak * @Contact reaching@techiecommunity.net * THESE SOURCE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESE * SOURCE CODE. */ package com.techienjoy; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import android.app.Activity; import android.app.ListActivity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class DirActivity extends ListActivity { private ListView listView; private SharedPreferences sharedPref; String dirList = null; @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.dir_menu_layout); listView = getListView(); final DirAdapter dirAdapter = new DirAdapter(this, new ArrayList()); sharedPref = getSharedPreferences("managefiles", Context.MODE_PRIVATE); Button butnSave = (Button) findViewById(R.id.butnDir); final EditText txtDir = (EditText) findViewById(R.id.menuEdtTxt); dirList = sharedPref.getString("managefiles", ""); dirAdapter.clear(); dirAdapter.notifyDataSetChanged(); StringTokenizer st = new StringTokenizer(dirList,":"); while(st.hasMoreTokens()) { String nextToken = st.nextToken(); dirAdapter.add(nextToken); } dirAdapter.notifyDataSetChanged(); butnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { if(sharedPref != null) { dirList = sharedPref.getString("managefiles", ""); String dirTxt = "/sdcard/"+txtDir.getText().toString(); if(!dirTxt.endsWith("/")) { dirTxt=dirTxt+"/"; } if(dirList.equals("")) { SharedPreferences.Editor editor = sharedPref.edit(); editor.putString("managefiles", dirTxt); editor.commit(); } else { if(!checkDuplicate(dirList, dirTxt)) { SharedPreferences.Editor editor = sharedPref.edit(); editor.putString("managefiles", dirList+":"+dirTxt); editor.commit(); } } } dirAdapter.clear(); dirAdapter.notifyDataSetChanged(); dirList = sharedPref.getString("managefiles", ""); StringTokenizer st = new StringTokenizer(dirList,":"); while(st.hasMoreTokens()) { String nextToken = st.nextToken(); dirAdapter.add(nextToken); } dirAdapter.notifyDataSetChanged(); } }); Button butnBack = (Button) findViewById(R.id.butnBack); butnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Intent intent= new Intent(DirActivity.this,ManageFiles.class); startActivity(intent); } }); setListAdapter(dirAdapter); getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long arg3) { dirList = deleteItem(dirList,position); Toast.makeText(getApplicationContext(), dirList, Toast.LENGTH_SHORT); Editor editor = sharedPref.edit(); editor.putString("managefiles",dirList); editor.commit(); dirList = sharedPref.getString("managefiles", ""); dirAdapter.clear(); dirAdapter.notifyDataSetChanged(); StringTokenizer st = new StringTokenizer(dirList,":"); while(st.hasMoreTokens()) { String nextToken = st.nextToken(); dirAdapter.add(nextToken); } dirAdapter.notifyDataSetChanged(); return true; } private String deleteItem(String dirList, int position) { String tmpStr = ""; StringTokenizer st = new StringTokenizer(dirList,":"); int i=0; while(st.hasMoreTokens()) { String temp1 = st.nextToken(); if(position != i) { tmpStr = tmpStr+temp1+":"; } i++; } return tmpStr; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.dir_menu_items, menu); MenuItem exitMenu = menu.getItem(0); exitMenu.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem arg0) { // Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); return true; } }); return true; } private boolean checkDuplicate(String str, String toFound){ StringTokenizer st = new StringTokenizer(str,":"); while(st.hasMoreTokens()) { String nextToken = st.nextToken(); if(nextToken != null && nextToken.equals(toFound)) { return true; } } return false; } } class DirAdapter extends ArrayAdapter { public DirAdapter(Context ctx, List list) { super(ctx,R.layout.dir_list_item,list); } }


Android-Projects-Manage-Files-Extension/src/com/techienjoy/ManageFiles.java

/** * @author Ishteak * @Contact reaching@techiecommunity.net * THESE SOURCE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESE * SOURCE CODE. */ package com.techienjoy; import java.io.File; import java.io.FilenameFilter; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.os.Bundle; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class ManageFiles extends Activity { private DropDownAdapter ddAdaptor; private SharedPreferences sharedPref; private Spinner edtText1; private Button btConvert; private Spinner edtExtTo; private EditText edtText; private CheckBox chkBox; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_manage_files); edtText1 = (Spinner) findViewById(R.id.spinner1); final Spinner edtExt1 = (Spinner) findViewById(R.id.spinner2); edtExtTo = (Spinner) findViewById(R.id.spinner3); sharedPref = getSharedPreferences("managefiles", Context.MODE_PRIVATE); String dirList = sharedPref.getString("managefiles", ""); ddAdaptor = new DropDownAdapter(getApplicationContext(), new ArrayList()); StringTokenizer st = new StringTokenizer(dirList,":"); while(st.hasMoreTokens()) { String nextToken = st.nextToken(); TextView txtView = new TextView(getApplicationContext()); txtView.setTextColor(Color.BLACK); txtView.setText(nextToken); ddAdaptor.add(nextToken); } edtText1.setAdapter(ddAdaptor); btConvert = (Button) findViewById(R.id.btChange); Button btClear = (Button) findViewById(R.id.btClear); btConvert.setEnabled(false); edtText1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { ddAdaptor.clear(); ddAdaptor.notifyDataSetChanged(); String dirList = sharedPref.getString("managefiles", ""); StringTokenizer st = new StringTokenizer(dirList,":"); while(st.hasMoreTokens()) { String nextToken = st.nextToken(); TextView txtView = new TextView(getApplicationContext()); txtView.setTextColor(Color.BLACK); txtView.setText(nextToken); ddAdaptor.add(nextToken); } ddAdaptor.notifyDataSetChanged(); return false; } }); edtExtTo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { if(!chkBox.isChecked()) btConvert.setEnabled(true); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); edtText1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { File file = new File(arg0.getSelectedItem().toString()); if(file.isDirectory()) { btConvert.setEnabled(true); } if(!file.isDirectory()) { btConvert.setEnabled(false); } file = null; } @Override public void onNothingSelected(AdapterView<?> arg0) { File file = new File(arg0.getSelectedItem().toString()); if(file.isDirectory()) { btConvert.setEnabled(true); } if(!file.isDirectory()) { btConvert.setEnabled(false); } file = null; } }); edtText = (EditText) findViewById(R.id.fileNameInit); chkBox = (CheckBox) findViewById(R.id.enableDisable); final Button btValidate = (Button) findViewById(R.id.btValidate); chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { if(arg1) { edtText.setEnabled(true); btValidate.setEnabled(true); btConvert.setEnabled(false); } else { edtText.setEnabled(false); btValidate.setEnabled(false); btConvert.setEnabled(true); } } }); btClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { if(chkBox.isChecked()) { edtText.setText(""); } } }); btValidate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { File file = new File(edtText1.getSelectedItem().toString()); if(file.isDirectory()) { File[] files = file.listFiles(); for(int i=0;i<files.length;i++) { //Toast.makeText(getApplicationContext(), edtText1.getSelectedItem().toString()+edtText.getText().toString()+i+edtExtTo.getSelectedItem().toString(), Toast.LENGTH_SHORT).show(); File tmpFile1 = new File(edtText1.getSelectedItem().toString()+edtText.getText().toString()+i+edtExtTo.getSelectedItem().toString()); if(tmpFile1.exists()) { btConvert.setEnabled(false); Toast.makeText(getApplicationContext(), "Already a File exists with this new file name..."+edtText1.getSelectedItem().toString()+edtText.getText().toString()+i+edtExtTo.getSelectedItem().toString(), Toast.LENGTH_SHORT).show(); break; } else { btConvert.setEnabled(true); } } } } }); edtText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View arg0, boolean arg1) { if(arg1) { btConvert.setEnabled(false); } } }); btConvert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { try { File file = new File(edtText1.getSelectedItem().toString()); if(file.isDirectory()) { if(validateFiles() || chkBox.isChecked()) { File[] files = file.listFiles(new FilenameFilter() { @Override public boolean accept(File arg0, String arg1) { if(arg1.toLowerCase().endsWith(edtExt1.getSelectedItem().toString())) { return true; } else { return false; } } }); for(int i=0;i<files.length;i++) { if(chkBox.isChecked() && (edtText.getText() != null && !edtText.getText().toString().equals(""))) { files[i].renameTo(new File(edtText1.getSelectedItem().toString()+edtText.getText().toString()+i+edtExtTo.getSelectedItem().toString())); } else { files[i].renameTo(new File(edtText1.getSelectedItem().toString()+files[i] .getName().substring(0, files[i].getName() .indexOf(".")) +edtExtTo.getSelectedItem().toString())); } } Toast.makeText(getApplicationContext(), "Done..."+files .length, Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getApplicationContext(),edtText1.getSelectedItem().toString() +" not a Directory", Toast.LENGTH_SHORT).show(); } } catch (Exception ex) { Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT).show(); } } }); } private boolean validateFiles() { File file1 = new File(edtText1.getSelectedItem().toString()); if(file1.isDirectory() && !chkBox.isChecked()) { File[] files = file1.listFiles(); for(int i=0;i<files.length;i++) { //Toast.makeText(getApplicationContext(), edtText1.getSelectedItem().toString()+edtText.getText().toString()+i+edtExtTo.getSelectedItem().toString(), Toast.LENGTH_SHORT).show(); File tmpFile1 = new File(edtText1.getSelectedItem().toString()+files[i] .getName().substring(0, files[i].getName() .indexOf(".")) +edtExtTo.getSelectedItem().toString()); if(tmpFile1.exists()) { btConvert.setEnabled(false); Toast.makeText(getApplicationContext(), "Already a File exists with this new file name...", Toast.LENGTH_SHORT).show(); return false; } } } return true; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_manage_files, menu); MenuItem dirMenu = menu.getItem(0); if(dirMenu != null) { dirMenu.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { Intent intent= new Intent(ManageFiles.this, DirActivity.class); startActivity(intent); return true; }}); } MenuItem exitMenu = menu.getItem(3); exitMenu.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem arg0) { // //finish(); //System.exit(0); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); return true; } }); return true; } } class DropDownAdapter extends ArrayAdapter { public DropDownAdapter(Context ctx, List list) { super(ctx,R.layout.dir_list_item,list); } }


If interested in commenting related to Android Projects: Manage Files Extension
Click Here

Name (To be shown on this Page:*

Email (Not to be shown):*

Website (To be shown on this Page)

Your Comments here (To be shown on this Page):*

(You can use <a> Tag in your comments)

Please enter same number as shown here 683




Android Thread Example

REST Interview Questions

JAX-WS Webservice Example

Android Overlap SurfaceView Example

Spring3 Integration of File Endpoint

JAXWS Webservice Example

Android WebView Example

Android Context Menu Example

Setting up Eclipse for Android App Development

Android SensorEvent Orientation Example

Android ImageSwitcher Example

REST API Design for Android App

Android ExpandableListView Example

Singleton Serializable on Android

Android GPS with Google MAP Example

Android Project With Example

Android Project Manageing Files

Project Ideas for App on Android Platform

Android EditText with Background

Android SQLite Database Example

Android AutoCompleteTextView Example

Android Tester

Android Interview Questions

Android ListView Example

Android Intent Notification Example

Android Intent Broadcast Example

Android TextView Link Example

Android Sensor List

Android Sensor Orientation Example

Android Intent Broadcast Receiver

Android Gallery SurfaceViews Example

Android Location Example

Android Text to Speech Example

Android Intent Example

Android Layout Example

Android Download File Example

Android Expandable ListView Example

Android Tab Example

Android RelativeLayout Example

Android Intent Example

Android Text to Speech Example

Android SQLite Example

Android CustomView Example

Android Canvas Example

Android SharedPreferences Example

Android Shape Background Example

Android ShapeButton Example

Android ViewFlipper Example

Android Gallery Example

Android TimePickerDialog Example





If interested commenting on this Page :

comments powered by Disqus
Any source code found on any of the page(s) in Techiecommunity.net is associated with following notice: 
   /*
     * TECHIECOMMUNITY.NET MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
     * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
     * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
     * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. TECHIECOMMUNITY.NET SHALL NOT BE LIABLE FOR
     * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
     * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
     *
     * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
     * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
     * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
     * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
     * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
     * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
     * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). TECHIECOMMUNITY.NET
     * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
     * HIGH RISK ACTIVITIES.
     */
   
The content provided in this page is NOT warranted and/or NOT guaranteed by techiecommunity.net .
techiecommunity.net is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

For any of the content, if you would like to bring it to notice for removal from this web site, 
please write to this web site administrator @ EMAIL-ID (please remove space before and after @),
with appropriate concern and supporting proof(s). After thorough review and if found genuine concern,
we would take appropriate action and remove disputed content from this web site within 24 hours 
starting from the time it has brought to techiecommunity.net Administrator notice.

This website uses cookies and third party cookies to improve the user experience. 
And to provide more relevant ads. 
In case of continued use of this website implies that you accept the use of cookies on this website.
		
While using this web site, you agree to have read and accepted techiecommunity.net terms of use and privacy policy.