I am developing an application which enables user to browse for a file in the storage (internal / external) and upload it to the java Serversocket.
Thank you.
Here is my file chooser code
public String fileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Choose Your File"), 1);
}
Application keeps crashing..... what am I doing wrong? Okay I figured it out the app was crashing because I was trying to return URI in the method itself. Then I modified it so that it could be obtained when user selects a file from the picker.
However now I am hosting a Socket server Which will receive the file (any file text/ multimedia) and store it into a particular directory.
My Receiver as follows:
public void step1()
{
File theDir = new File("C://ServerFiles");
if (!theDir.exists()) {
try{
theDir.mkdir();
} catch(SecurityException se){
areaStatus.append("error creating file \n");
}
}
try {
InetAddress IP=InetAddress.getLocalHost();
lblIp.setText("IP:"+IP.toString());
} catch (UnknownHostException ex) {
Logger.getLogger(ServerConfig.class.getName()).log(Level.SEVERE, null, ex);
}
try {
areaStatus.append("ServerRunning.... \n");
String SPort;
SPort = txtport.getText();
Serverport =Integer.parseInt(SPort);
serverSocket = new ServerSocket(Serverport);
ClientSocket=serverSocket.accept();
areaStatus.append("Connected With"+ClientSocket.getInetAddress().toString()+"\n");
input = ClientSocket.getInputStream();
BufferedReader inReader = new BufferedReader(new InputStreamReader(ClientSocket.getInputStream()));
BufferedWriter outReader = new BufferedWriter(new OutputStreamWriter(ClientSocket.getOutputStream()));
filename = inReader.readLine();
if ( !filename.equals("") ){
outReader.write("READY\n");
outReader.flush();
}
} catch (IOException ex) {
Logger.getLogger(ServerConfig.class.getName()).log(Level.SEVERE, null, ex);
}
areaStatus.append("Client connected \n");
}
public void step2()
{
byte[] buffer;
buffer= null;
FileOutputStream wr = null;
try {
wr = new FileOutputStream(new File("C://ServerFiles/" + filename));
try {
buffer = new byte[ClientSocket.getReceiveBufferSize()];
} catch (SocketException ex) {
Logger.getLogger(ServerConfig.class.getName()).log(Level.SEVERE, null, ex);
}
int bytesReceived = 0;
try {
while((bytesReceived = input.read(buffer))>0)
{
/* Write to the file */
wr.write(buffer,0,bytesReceived);
}
} catch (IOException ex) {
Logger.getLogger(ServerConfig.class.getName()).log(Level.SEVERE, null, ex);
}
try {
wr.close();
} catch (IOException ex) {
Logger.getLogger(ServerConfig.class.getName()).log(Level.SEVERE, null, ex);
}
areaStatus.append("Transfer completed \n");
} catch (FileNotFoundException ex) {
Logger.getLogger(ServerConfig.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
wr.close();
} catch (IOException ex) {
Logger.getLogger(ServerConfig.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
step1();
}
}
}
I am working on file sender I'll post as soon as I'm done.
This is my file sender but it is showing File not found error:
private class Fileuploader extends AsyncTask<Void, Void, Integer> {
protected void onPreExecute() {
}
protected Integer doInBackground(Void... params) {
Socket sk;
try {
sk = new Socket("192.168.0.108", 1131);
File myFile = new File(filepathString);
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 = sk.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
sk.close();
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}
protected void onPostExecute(Integer result) {
}
}
My ADB log:
04-22 13:23:14.269 8355-8385/envoyone.ak.ace.fileud I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-22 13:23:14.299 8355-8385/envoyone.ak.ace.fileud D/OpenGLRenderer﹕ Enabling debug mode 0
04-22 13:23:16.860 8355-8355/envoyone.ak.ace.fileud W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
04-22 13:23:24.745 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ java.io.FileNotFoundException: content:/com.android.externalstorage.documents/document/primary%3Atext%2Fmessenger_dndictionary.xml: open failed: ENOENT (No such file or directory)
04-22 13:23:24.757 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:456)
04-22 13:23:24.757 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:76)
04-22 13:23:24.757 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ at envoyone.ak.ace.fileud.FileUpload$Fileuploader.doInBackground(FileUpload.java:98)
04-22 13:23:24.757 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ at envoyone.ak.ace.fileud.FileUpload$Fileuploader.doInBackground(FileUpload.java:78)
04-22 13:23:24.757 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-22 13:23:24.757 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-22 13:23:24.757 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-22 13:23:24.759 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-22 13:23:24.759 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-22 13:23:24.759 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
04-22 13:23:24.759 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
04-22 13:23:24.760 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ at libcore.io.Posix.open(Native Method)
04-22 13:23:24.760 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
04-22 13:23:24.760 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:442)
04-22 13:23:24.760 8355-8387/envoyone.ak.ace.fileud W/System.err﹕ ... 9 more
04-22 13:23:54.924 8355-8355/envoyone.ak.ace.fileud W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection