I am getting an error while file uploading to the server from the real device. All are working well in the emulator as I switch to real device get an error.
Here is code,
Function to choose a file from the device
private void showFileChooser(int index) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
index);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(), "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
OnResultActivityCode :
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedFileURI = data.getData();
File file = new File(selectedFileURI.getPath().toString());
Log.i("", "File : " + file.getName());
uploadedFileName = file.getName().toString();
tokens = new StringTokenizer(uploadedFileName, ":");
first = tokens.nextToken();
file_1 = tokens.nextToken().trim();
}
}
}
Code to upload file :
@Override
protected String doInBackground(String... strings) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("URL");
if (file_1 != null && !file_1.equalsIgnoreCase("")) {
file1 = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(), file_1);
fileBody1 = new FileBody(file1);
}
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
if (file_1 != null && !file_1.equalsIgnoreCase(""))
reqEntity.addPart("file", fileBody1);
httpPost.setEntity(reqEntity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
final String responseStr = EntityUtils.toString(resEntity)
.trim();
Log.v(TAG, "Response: " + responseStr);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
parseData(responseStr);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Here is error that I got when try to do with real device.
12-26 12:47:32.919: W/System.err(15529): java.io.FileNotFoundException: /storage/emulated/0/Woodenstreet Doc.doc: open failed: ENOENT (No such file or directory) 12-26 12:47:32.920: W/System.err(15529): at libcore.io.IoBridge.open(IoBridge.java:491) 12-26 12:47:32.920: W/System.err(15529): at java.io.FileInputStream.(FileInputStream.java:76) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.entity.mime.content.FileBody.writeTo(FileBody.java:92) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.entity.mime.HttpMultipart.doWriteTo(HttpMultipart.java:206) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.entity.mime.HttpMultipart.writeTo(HttpMultipart.java:224) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.entity.mime.MultipartEntity.writeTo(MultipartEntity.java:183) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:97) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:162) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.impl.conn.AbstractClientConnAdapter.sendRequestEntity(AbstractClientConnAdapter.java:272) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:242) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:592) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:512) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:490) 12-26 12:47:32.921: W/System.err(15529): at com.cognus.gha.fragments.Fragment_Chat$PostDataAsyncTask.doInBackground(Fragment_Chat.java:498) 12-26 12:47:32.921: W/System.err(15529): at com.cognus.gha.fragments.Fragment_Chat$PostDataAsyncTask.doInBackground(Fragment_Chat.java:1) 12-26 12:47:32.921: W/System.err(15529): at android.os.AsyncTask$2.call(AsyncTask.java:288) 12-26 12:47:32.921: W/System.err(15529): at java.util.concurrent.FutureTask.run(FutureTask.java:237) 12-26 12:47:32.921: W/System.err(15529): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 12-26 12:47:32.921: W/System.err(15529): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 12-26 12:47:32.921: W/System.err(15529): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 12-26 12:47:32.921: W/System.err(15529): at java.lang.Thread.run(Thread.java:818) 12-26 12:47:32.921: W/System.err(15529): Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory) 12-26 12:47:32.921: W/System.err(15529): at libcore.io.Posix.open(Native Method) 12-26 12:47:32.921: W/System.err(15529): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) 12-26 12:47:32.921: W/System.err(15529): at libcore.io.IoBridge.open(IoBridge.java:477) 12-26 12:47:32.921: W/System.err(15529): ... 22 more
Error -
12-26 12:47:32.919: W/System.err(15529): java.io.FileNotFoundException: /storage/emulated/0/Woodenstreet Doc.doc: open failed: ENOENT (No such file or directory)
I am not able to whats the problem is that. Please help.