0

I'm trying to upload a picture from Android phone's DCIM folder using JSCH SFTP. This is the main part of the code:

channelSftp2.put("/storage/emulated/0/DCIM/Camera/IMG_20180118_122224.jpg","/home/pi/IMG_20180118_122224.jpg");

But unfortunately it doesn't work. The logcat does not give any specific errors. I changed /storage/emulated/0/DCIM/Camera/IMG_20180118_122224.jpg to /sdcard/DCIM/Camera/IMG_20180118_122224.jpg but nothing changed.

I'm writing the codes in Android Studio 2.2.2 and the build SDK is 24. I have added READ_EXTERNAL_STORAGE permission to the AndroidManifest.xml file.

The following is the full code snippet:

        new AsyncTask<Integer, Void, Void>(){   

            @Override
            protected Void doInBackground(Integer... params) {

                String SFTPHOST2 = "192.168.0.1";
                int SFTPPORT2 = 22;
                String SFTPUSER2 = "pi";
                String SFTPPASS2 = "raspberry";
                String SFTPWORKINGDIR2 = "/home/pi/";

                Session session2 = null;
                Channel channel2 = null;
                ChannelSftp channelSftp2 = null;

                try {
                    JSch jsch2 = new JSch();
                    session2 = jsch2.getSession(SFTPUSER2, SFTPHOST2, SFTPPORT2);
                    session2.setPassword(SFTPPASS2);
                    session2.setConfig("StrictHostKeyChecking", "no");
                    session2.setTimeout(10000);
                    while (!session2.isConnected())
                        session2.connect();

                    channel2 = session2.openChannel("sftp");
                    while (!channel2.isConnected())
                        channel2.connect();
                    channelSftp2 = (ChannelSftp) channel2;
                    channelSftp2.cd(SFTPWORKINGDIR2);

                    channelSftp2.put("/storage/emulated/0/DCIM/Camera/IMG_20180118_122224.jpg","/home/pi/IMG_20180118_122224.jpg");

                } catch (Exception ex) {
                    ex.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error Connecting", Toast.LENGTH_LONG).show();
                }
                return null;
            }

        }.execute(1);

So, what is the problem !? How can I properly upload a file from Android phone to a remote server using JSCH SFTP !?

Omid1989
  • 419
  • 3
  • 8
  • 17
  • *"it doesn't work"* doesn't work as problem description. What does it do? + Show us JSch log file: https://stackoverflow.com/q/26826894/850848 + Can you upload a file to the same path on the server using the same credentials using any standalone SFTP client (like WinSCP)? If you can, show us its log file. – Martin Prikryl Jan 18 '18 at 09:53
  • Thanks @MartinPrikryl for your comment. I mean it does not transfer the file to the server. I've tried pscp on windows which works perfectly (Its log says 100% transferred successful). – Omid1989 Jan 18 '18 at 10:14
  • How do you verify that *"it does not transfer the file to the server"*? Did you do the same verification with `pscp`? + Show us your `pscp` commandline. – Martin Prikryl Jan 18 '18 at 10:47
  • @MartinPrikryl I use `SSH` with `putty` to see whether the file has been transferred or not. – Omid1989 Jan 18 '18 at 10:56
  • You have answered only 1 of my 3 questions. – Martin Prikryl Jan 18 '18 at 11:08

0 Answers0