I am developing an Android app for finding duplicate files. till now i have developed directory explorer activity. user will select the directory through this activity, then i am starting a service in separate process to list all files in that directory (using DFS algorithm) and to find all the duplicates among them.
for now i want to show the list of all the files in that directory and its sub-directory in separate activity. i am using ArrayList to hold the file object and want to pass this ArrayList from this service to Activity. I am writing the ArrayList to a file "filelist.bin" in the service and want to read that file from the Activity and here i am getting the error !! .
Also for launching the activity i am using Notification which is thrown by service when it has complete listing all the files and adding them to ArrayList.
i am getting Resource not found exception when i am trying to read the file. All the error details is listed at the end.
public class BgService extends Service {
static String DIR0;
//public static ArrayList<FileX> OnlyFiles = new ArrayList<FileX>();
public static Stack<File> dirStack = new Stack<File>();
public static File file0;
public static File[] filearray;
static int size=0;
static FileX file2;
public static ArrayList<File> sOnlyFiles = new ArrayList<File>();
ArrayList<File> ListoFile;
static String hash1,hash2;
static ArrayList<Integer> Index2Del= new ArrayList<Integer>();
ArrayList<FileX> localOF;
FileOutputStream fos ;
FileInputStream fis;
ObjectOutputStream obs;
ObjectInputStream obsi;
@Override
public void onCreate() {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "service created",5000).show();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
DIR0 = new String( intent.getCharArrayExtra("dir0"));
listAllfiles();
Collections.sort(sOnlyFiles, SizeFileComparator.SIZE_COMPARATOR);
serializeList();
//deserializeList();
/*
int size = ListoFile.size();
for(int i = 0;i<size;i++){
Toast.makeText(getApplicationContext(),String.valueOf(ListoFile.get(i).length()),1000).show();
}*/
displayNotification();
this.stopSelf();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "service destroyed",5000).show();
super.onDestroy();
}
public void listAllfiles(){
dirStack.push(new File(DIR0));
while(dirStack.size()!=0){
file0 = dirStack.pop();
filearray=file0.listFiles();
for(File lookFile : filearray){
if(lookFile.isFile() && lookFile.canRead()){
sOnlyFiles.add(lookFile);
DataHolder.Dcounter++;
}
else if(lookFile.isDirectory()&& lookFile.canRead()){
dirStack.push(lookFile);
}
}
}
}
public void serializeList() {
String filename = "filelist.bin";
File dir = getDir("DFR", MODE_PRIVATE);
try {
Resources res = Resources.getSystem();
fos = openFileOutput(filename, MODE_PRIVATE);
obs = new ObjectOutputStream(fos);
obs.writeObject(sOnlyFiles);
obs.flush();
obs.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void displayNotification(){
NotificationManager nm = (NotificationManager)getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
Intent i = new Intent(getApplicationContext(),AllFiles.class);
// Bundle listfile = new Bundle();
//listfile.putStringArrayList("listfile", sOnlyFiles);
//i.putExtra(name, value)
//i.putExtra("filelist", sOnlyFiles);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
Notification nf = new Notification(R.drawable.ic_launcher,"Duplicate File Remover\n"+ DataHolder.Dcounter+" files found",System.currentTimeMillis());
nf.setLatestEventInfo(getApplicationContext(), "Duplicate File Remover", DataHolder.Dcounter+" files found",pi);
nf.vibrate = new long[]{100,250,100,500};
nm.notify(1,nf);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
public class AllFiles extends ListActivity{
FileInputStream fis ;
ObjectInputStream obs;
public static ArrayList<File> ListoFiles;
@Override
protected void onCreate(Bundle savedInstanceState) {
stopService(new Intent(this, BgService.class));
deserializeList();
// stopService(new Intent(this, BgService.class));
//setListAdapter(new OnlyFilesAdapter());
//Bundle filelist = getIntent().getExtras();
super.onCreate(savedInstanceState);
}
public void deserializeList(){
Object tolist =null;
String filename = "filelist.bin";
try {
fis = openFileInput(filename);
if(fis!=null){
obs = new ObjectInputStream(fis);
tolist = obs.readObject();
@SuppressWarnings("unchecked")
ArrayList<File> tolist2 = (ArrayList<File>)tolist;
ListoFiles = tolist2;
obs.close();
fis.close();
Toast.makeText(getApplicationContext(), ListoFiles.size(), 5000).show();
}else
Toast.makeText(getApplicationContext(), "file not available", 2000).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
LogCat Values on Error :-
10-14 13:30:33.018: W/ResourceType(433): No package identifier when getting value for resource number 0x0000001b 10-14 13:30:33.070: D/AndroidRuntime(433): Shutting down VM 10-14 13:30:33.070: W/dalvikvm(433): threadid=1: thread exiting with uncaught exception (group=0x40015560) 10-14 13:30:33.269: E/AndroidRuntime(433): FATAL EXCEPTION: main 10-14 13:30:33.269: E/AndroidRuntime(433): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.com.filebrowser/my.com.filebrowser.AllFiles}: android.content.res.Resources$NotFoundException: String resource ID #0x1b 10-14 13:30:33.269: E/AndroidRuntime(433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 10-14 13:30:33.269: E/AndroidRuntime(433): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 10-14 13:30:33.269: E/AndroidRuntime(433): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 10-14 13:30:33.269: E/AndroidRuntime(433): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 10-14 13:30:33.269: E/AndroidRuntime(433): at android.os.Handler.dispatchMessage(Handler.java:99) 10-14 13:30:33.269: E/AndroidRuntime(433): at android.os.Looper.loop(Looper.java:123) 10-14 13:30:33.269: E/AndroidRuntime(433): at android.app.ActivityThread.main(ActivityThread.java:3683) 10-14 13:30:33.269: E/AndroidRuntime(433): at java.lang.reflect.Method.invokeNative(Native Method) 10-14 13:30:33.269: E/AndroidRuntime(433): at java.lang.reflect.Method.invoke(Method.java:507) 10-14 13:30:33.269: E/AndroidRuntime(433): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 10-14 13:30:33.269: E/AndroidRuntime(433): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 10-14 13:30:33.269: E/AndroidRuntime(433): at dalvik.system.NativeStart.main(Native Method) 10-14 13:30:33.269: E/AndroidRuntime(433): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1b 10-14 13:30:33.269: E/AndroidRuntime(433): at android.content.res.Resources.getText(Resources.java:201) 10-14 13:30:33.269: E/AndroidRuntime(433): at android.widget.Toast.makeText(Toast.java:258) 10-14 13:30:33.269: E/AndroidRuntime(433): at my.com.filebrowser.AllFiles.deserializeList(AllFiles.java:60) 10-14 13:30:33.269: E/AndroidRuntime(433): at my.com.filebrowser.AllFiles.onCreate(AllFiles.java:34) 10-14 13:30:33.269: E/AndroidRuntime(433): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 10-14 13:30:33.269: E/AndroidRuntime(433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 10-14 13:30:33.269: E/AndroidRuntime(433): ... 11 more
In this line of code:
Toast.makeText(getApplicationContext(), ListoFiles.size(), 5000).show();
ListoFiles.size()
returns in int
. If you call
Toast.makeText(Context context, int resId, int duration)
then the API expects the second paramter to be the ID of a string resource. In your case it isn't. If you just want to show a toast with a number in it, try this:
Toast.makeText(getApplicationContext(), "" + ListoFiles.size(), 5000).show();
or
Toast.makeText(getApplicationContext(), Integer.toString(ListoFiles.size()), 5000).show();
User contributions licensed under CC BY-SA 3.0