从真实设备上传到服务器的文件时出现错误。当我切换到真实设备获取错误时,所有这些都在模拟器中运行良好。
这是代码,
从设备中选择文件的功能
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();
}
}
}
上传文件的代码:
@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;
}
这是我在尝试使用真实设备时遇到的错误。
> 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
错误 -
12-26 12:47:32.919:W / System.err(15529):java.io.FileNotFoundException:/ storage / emulated / 0 / Woodenstreet Doc.doc:open failed:ENOENT(没有这样的文件或目录)
我不知道问题是什么。请帮忙。
答案 0 :(得分:0)
看来你的文件路径错了。尝试使用此代码获取文件路径,然后更改代码并运行。
使用此代码获取文件路径。
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;
}
你的onActivityResult()代码的变化如下:
getRealPathFromUri(getActivity(), selectedFileURI);
最后在doInBackgraound()方法中使用它
if (file_1 != null && !file_1.equalsIgnoreCase("")) {
file1 = new File(picturePath);
fileBody1 = new FileBody(file1);
}
希望这会对你有所帮助。