我正在尝试上传不在app文件夹下但在公共文件夹下的图片。
为了上传图片,我正在使用下一个代码 -
private String doPicUpload(String path1){
File file1 = new File(path1);
String urlString = "http://87.182.1.15/upload_pic.php";
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlString);
FileBody bin1 = new FileBody(file1);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("uploadedfile1", bin1);
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
resEntity = response.getEntity();
final String response_str = EntityUtils.toString(resEntity);
return response_str;
} catch (Exception ex){
Log.e("Debug", "error: " + ex.getMessage(), ex);
String expction = "0";
return expction;
}
}
现在,当我调试此代码时,它似乎停在此行 -
HttpResponse response = client.execute(post);
logcat也说 -
03-21 14:13:16.074: E/Debug(26180): error: /file:/storage/emulated/0/MyDir/img_1395403976349.jpg: open failed: ENOENT (No such file or directory)
03-21 14:13:16.074: E/Debug(26180): java.io.FileNotFoundException: /file:/storage/emulated/0/MyDir/img_1395403976349.jpg: open failed: ENOENT (No such file or directory)
现在你可以看到图像的路径很好,在设备上有一个同名的文件。
那我在这里做错了什么? 我应该参考MEDIA环境,如果是这样,我该怎么办?
感谢您提供任何帮助
答案 0 :(得分:0)
将以下内容添加到清单文件*
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
并尝试使用Environment.getExternalStorageDirectory()
获取文件路径
您的路径应如下/storage/emulated/0/MyDir/img_1395403976349.jpg