0

my app generates text files and store them in internal storage. there is no problem in reading and writing files but it was throwing exception File not found(no such file or directory ) when i try to send file to laptop using(wlan) ftp.

then after debugging i got that it was a problem of file path. so i hard-coded file path like this

create and write file

 String path=context.getFilesDir().getAbsolutePath();

             File file = new File(path + File.separator + fileName);
             RandomAccessFile rf = new RandomAccessFile(file,"rws"); 
             file.getParentFile().mkdirs();
                Log.d("Created In",path);// in log path i found "/data/data/com.meta.Myapp/files"

                byte[] text = new byte[(int) file.length()];
                rf.readFully(text);
                rf.seek(0);
                rf.writeBytes(data);
                rf.write(text);
                Log.d("write","writing file...");
                rf.close();

client side code to send file

  sock = new Socket("192.168.2.2", 21); 
          // sendfile

                String path="/data/data/com.meta.Myapp/files";

                Log.d("Fetching From",path);

                File myFile = new File ( path+ File.separator + fname); 
                byte [] mybytearray  = new byte [(int)myFile.length()];
                FileInputStream fis = new FileInputStream(myFile);
                BufferedInputStream bis = new BufferedInputStream(fis);
                bis.read(mybytearray,0,mybytearray.length);
                OutputStream os = sock.getOutputStream();
                Log.d("Sending","Sending in progress");
                os.write(mybytearray,0,mybytearray.length);
                os.flush();

              sock.close();

now this is working fine with no problem. but i should not use hardcoded file path. so how to get filepath to fetch file. as client part runs in AsyncTask<> i cant use Context.getFilesDir().getPath() and i dont know how to pass context to AsyncTask<> so how do i solve this problem

jaimin
  • 563
  • 8
  • 25

0 Answers0