Saturday, 15 May 2010

java - Using internal files with AsyncTask -



java - Using internal files with AsyncTask -

i'm developing application send internal file (of application) on network sockets. if utilize asynctask that, file cannot read. in main class can normaly read it. need asynctask because want utilize progressdialog (progressbar). help.

public class send{ file f; context c; public send(context context){ c.this = context; //some code f = new file(context.getfilesdir(), "pom.txt"); system.out.println(f.canread() + " " + f.lenght()); //true , >0 new sendfile(context).execute(); } private class sendfile extends asynctask<void, integer, void> { context ca; public sendfile(context context){ this.ca = context; } @override protected void doinbackground(void... arg0) { try{ //some code , open socket system.out.println(f.canread() + " " + f.lenght()); //false , ==0 f = new file(ca.getfilesdir(), "pom.txt"); //or same thing with: f = new file(c.getfilesdir(), "pom.txt"); system.out.println(f.canread() + " " + f.lenght()); //false , ==0 //some code fileinputstream fis = new fileinputstream(f); //null //code sending file fis.close(); s.close(); } grab (ioexception e) {} homecoming null; } } }

whole code (as requested):

public class send{ inetaddress adresa = null; context con; public send(context context) { this.con = context; adresa = auth.getipadr(); //get ip new sendfile(context).execute(); } private class sendfile extends asynctask<void, integer, void>{ progressdialog popup; context c; public sendfile(context context){ this.c = context; } @override protected void doinbackground(void... arg0) { try{ socket s = new socket(adresa,95123); file f = new file(c.getfilesdir(), "pom.txt"); //internal file ... null byte[] polje = new byte[65536]; int len; fileinputstream fis = new fileinputstream(f); //null dataoutputstream dos = new dataoutputstream(new bufferedoutputstream(s.getoutputstream())); dos.writeutf(f.getname()); while((len = fis.read(polje)) != -1){ dos.write(polje, 0, len); } dos.flush(); dos.close(); fis.close(); s.close(); } grab (ioexception e) { e.printstacktrace(); } homecoming null; } @override protected void onpostexecute(void result) { // todo auto-generated method stub super.onpostexecute(result); popup.dismiss(); } @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); popup = new progressdialog(context); popup.setprogressstyle(progressdialog.style_horizontal); popup.setmessage("sending files..."); popup.settitle("sending"); popup.setindeterminate(false); popup.show(); } @override protected void onprogressupdate(integer... values) { // todo auto-generated method stub super.onprogressupdate(values); popup.setprogress(values[0]); } } }

java android sockets android-asynctask

No comments:

Post a Comment