One of the tasks of my training project is to transfer a file from an Android smartphone to an FTP server. Googling, I found that this can be done using the Apache Commons library. The library successfully connected to the project, added a resolution to AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" /> 

Further, I used the example of uploading a file to an FTP server:

 public static void ftpConn(String hostAddress, String log, String password) throws FileNotFoundException { FTPClient fClient = new FTPClient(); FileInputStream fInput = new FileInputStream("Android/data/название_пакета/Yes.txt"); String fs = "Yes.txt"; try { fClient.connect(hostAddress); fClient.enterLocalPassiveMode(); fClient.login(log, password); fClient.storeFile(fs, fInput); fClient.logout(); fClient.disconnect(); } catch (IOException ex) { System.err.println(ex); } } 

The event handler on the button, on clicking on which the file will be sent:

 try { ftpConn("n***.n****.ru","*****","*****"); } catch (FileNotFoundException ex) { Toast.makeText(getApplicationContext(), "Неудача", Toast.LENGTH_SHORT).show(); } 

The file that I need to send I put in the internal memory of the device: Android / data / package_name / Yes.txt. I tried to prescribe various file paths into the string:

 FileInputStream fInput = new FileInputStream("Android/data/название_пакета/Yes.txt"); 

But nothing worked. The application caught an exception and displayed Toast: "Failure."

Tell me, please, what's the problem? I think the error is that I incorrectly specify the path to the file.

PS: Launched on Nexus 5 with Android 6.

PSS: This example of sending a file to FTP tried to run on a computer - everything worked out the first time.

  • Show the stack trace. - s8am
  • I think you even have two problems: you really specify the wrong path, you must specify full, something like "/mnt/sdcard0/Android/data/название_пакета/Yes.txt" ; Well, when you find the right path to the file, your application will most likely need read access to external files READ_EXTERNAL_STORAGE , and starting with android 6, these rights must not only be written in the manifest, but checked and, if necessary, requested before reading the file. - xkor

2 answers 2

In FileInputStream , either the relative path to the file (from the root of the file system of your project) or the absolute path must be passed. Absolute begins with a slash / and indicates the full path to the file from the root of the device file system.

You now have a relative path (without a slash in the beginning) but clearly not correct.

As far as I understand, the application does not have access to other locations on the internal memory, except for the directory of the application itself. Therefore, the file must be in the project.

You can get the absolute path to the file like this:

 String yourFilePath = context.getFilesDir() + "/" + "hello.txt"; 

and use it as follows:

 FileInputStream fInput = new FileInputStream(yourFilePath); 

On my test project, I getApplicationContext().getFilesDir() returned

/data/user/0/com.example.note.test/files

  • And do not tell me how the application to access other locations on the internal memory? Incidentally, not by permission in the manifest: "<uses-android: name =" android.permission.READ_EXTERNAL_STORAGE "/>"? - osip_000
  • @AndreiO. Look here: developer.android.com/training/basics/data-storage/files.html Your application will not be able to get permissions to read any files in the internal memory. It's crazy - all the system files and files of other applications ... you have access to external storage and a "piece" of internal storage assigned to you (well, other by-catch files in the internal storage, which the owners have specially marked available to others). For the rest, you need root access. - Ivan Pshenitsyn

Use Context.getFilesDir() to get the path to the device’s internal memory

For example:

 FileInputStream fInput = new FileInputStream(getFilesDir() + "/Android/data/название_пакета/Yes.txt");