我正在尝试使用 http post 进行文件上传,但是当我尝试使用手机上的 micro-sd 卡中的文件时,它不会创建文件,因此我无法上传它。从主要手机存储中选择文件时,它可以完美运行,而不是从 SD 卡中选择文件。
我在文件选择对话框中使用了一个外部库,当我尝试从 SD 卡中选择一个图像时它返回这个(在这个例子中它是从相机应用程序拍摄的照片):
/sdcard-ext/dcim/Camera/2012-07-02_12-28-19_548.jpg
这是从我在手机操作系统文件中找到的随机图像返回的:
/sdcard/DCIM/.thumbnails/1331049921270.jpg
我知道这个看起来也像是在 SD 卡上(或者至少对我来说是这样),但这绝对是手机的存储空间。
这是我目前用于文件上传的代码: HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(myURL);
MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
String mimeType = getFileMimeType(filePath);//the filePath is passed to the function
that this code is contained inside and is initially sent from the file selection dialog
mp.addPart("fileupload", new FileBody(new File(filePath), mimeType));
httppost.setEntity(mp);
String response = EntityUtils.toString( httpclient.execute( httppost ).getEntity(), "UTF-8" );
System.out.println(response);
httpclient.getConnectionManager().shutdown();
如果有人能解释为什么这适用于手机的内部存储而不适用于可移动 SD 卡,那就太好了! 谢谢。
更新:我的目标是 Android 2.2,如果这有什么不同的话。如果没有任何区别,我仍然以 Android 2.2 为目标
最佳答案
添加
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
注意:您还应该在 logcat 中看到“权限被拒绝”错误,这应该提醒您注意这个问题。
编辑: 抱歉,要从您需要的外部存储中读取:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
关于从 SD 卡加载 Android 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11300121/