【发布时间】:2018-03-08 14:22:14
【问题描述】:
我想使用多部分请求将 pdf 发送到我的服务器。我能够正确选择文件并获取其名称,但是当我发送此 pdf 时,我正在发送以下路径 /document/raw:/storage/emulated/0/Download/kitchenapp.pdf。路径是正确的,文件在那里,但我得到了这个异常。 I/DefaultRequestDirector: I/O exception (java.io.FileNotFoundException) caught when processing request: /document/raw:/storage/emulated/0/Download/kitchenapp.pdf (No such file or directory)
到目前为止我做了什么.. 通过这个获取 pdf
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
1);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(), "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
由此获取onActivity结果
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri selectedFileURI = data.getData();
file = new File(selectedFileURI.getPath().toString());
Log.d("", "File : " + file.getName());
String uploadedFileName = file.getName().toString();
System.out.println("upload file name "+uploadedFileName);
System.out.println("my location "+file);
}
}
通过多部分请求发送此文件
if (file != null ) {
entity.addPart("file", new FileBody(file));
}
// totalSize = entity.getContentLength();
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
任何帮助将不胜感激..
【问题讨论】:
-
您是否在 Manifest 中添加了权限?
-
我已经声明了 API 23 或更高版本的清单和运行时权限,正如我所提到的,我也可以获取文件名。但我认为问题出在路径上。所以在上传时它的显示文件未找到异常。 @Ankita
标签: android pdf android-contentprovider filepath