问题
I am getting error while file uploading to server from real device. All are working good in emulator as I switch to real device get error.
Here is code,
Function to choose file from 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.<init>(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.
回答1:
It seems you getting wrong file path. Try this code to get file path and then change code and it works.
Use this code for get path of file.
public static String getRealPathFromUri(Context ctx, Uri uri) {
String[] filePathColumn = { MediaStore.Files.FileColumns.DATA };
Cursor cursor = ctx.getContentResolver().query(uri, filePathColumn,
null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
Log.e("", "picturePath : " + picturePath);
cursor.close();
return picturePath;
}
And the change in your onActivityResult() code like this :
getRealPathFromUri(getActivity(), selectedFileURI);
And finally use this in doInBackgraound() method
if (file_1 != null && !file_1.equalsIgnoreCase("")) {
file1 = new File(picturePath);
fileBody1 = new FileBody(file1);
}
Hope this will help you.
来源:https://stackoverflow.com/questions/34469351/getting-error-while-uploading-file-from-real-device-to-server