In the below program i get the exception as shown below.Basically what i am trying to do is i am copying a file from a remote system to the device.but when saving to the memory card both on the emulator and device i am hitting an exception.The below exception is from the emulator where i can say that the memory card is not attached to the emulator and so the exception.but will the code work for a physical device.
How to make the code work on emulator and the device
Exception:
09-13 15:47:16.789: I/System.out(400): java.io.FileNotFoundException: /mnt/sdcard/Download/new.txt (Not a directory)
Code:
package com.scp2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.jcraft.jsch.*;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
public class Scp2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession("guest", "17.30.5.2", 22);
session.connect();
Toast.makeText(this, "in try2" , Toast.LENGTH_SHORT).show();
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
File ofile = new File(Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DOWNLOADS),"new.txt");
Toast.makeText(this, ofile.toString() , Toast.LENGTH_SHORT).show();
try {
FileOutputStream f = new FileOutputStream(ofile);
sftpChannel.get("/root/a.txt","/mnt/sdcard/download/dd.txt");
/*OR What shoud the abobe statement be **/
//sftpChannel.get("/root/a.txt",ofile); or this statement is correct
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println(e.toString());
}
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
}
Do this for file creation ; a better and safer way than yours