gpt4 book ai didi

android - 打开失败 : ENOENT (No such file or directory) when uploading an image to a website using POST

转载 作者:太空狗 更新时间:2023-10-29 12:49:43 24 4
gpt4 key购买 nike

我将图像的 Uri 字符串作为字符串保存到 SQLite。我可以通过为其创建查看 Intent 来查看图像。

但是当我使用 FileBody(new File(theUriFromTheDatabase)) 将图像上传到网络服务器时,它总是说“打开失败:ENOENT(没有这样的文件或目录)”

文件的 uri 是:“/content:/media/external/images/media/667”

事实:

  1. 我确定文件在那里,因为我可以查看它
  2. 我启用了内部存储读/写、外部存储读/写权限
  3. 使用 Galaxy Tab 2 10.1
  4. 它没有 SD 卡
  5. 相同的代码适用于我的带有 SD 卡的 Experia Neo V(是因为它没有 SD 卡吗?)
  6. 尝试在启动应用程序之前拔掉电线
  7. 有线 USB 已关闭

代码如下:

    protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub

InspectionsDbController db = new InspectionsDbController(getActivity());

InspectionItemStruct[] ins = db.getInspectionList(((MyApplication)((Activity) mContext).getApplication()).getCurrentInspectionId());

SharedPreferences settings = mContext.getSharedPreferences(MainActivity.PREFS_NAME, 0);
long userId = settings.getLong("userId", 0);
String token = settings.getString("token", "");

for (int i = 0; i < ins.length; i++) {


HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://webprojectupdates.com/mmp/api/mobile/upload_inspection");


MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

// add userId
try {
entity.addPart("userId", new StringBody(String.valueOf(userId)));
entity.addPart("token", new StringBody(String.valueOf(token)));

} catch (IOException e) {
Log.e("MMP","Error in adding token: "+e.getMessage());
}




// add media attachments
if(ins[i].image!=null){


//Bitmap image = BitmapFactory.decodeFile(ins[i].image);

//ByteArrayOutputStream bos = new ByteArrayOutputStream();
//image.compress(CompressFormat.JPEG, 75, bos);
//byte[] imageData = bos.toByteArray();
//ByteArrayBody bab = new ByteArrayBody(imageData,"image/jpg", ins[i].itemId+".jpg");

//entity.addPart("image", bab);
entity.addPart("image", new FileBody(new File (ins[i].image)));
}
if(ins[i].video!=null){
entity.addPart("video", new FileBody(new File (ins[i].video)));
}

// Normal string data
try {
entity.addPart("itemId", new StringBody(String.valueOf(ins[i].itemId)));
entity.addPart("isExist", new StringBody(String.valueOf(ins[i].itemExists)));
if(ins[i].comments!=null) entity.addPart("comment", new StringBody(String.valueOf(ins[i].comments)));
entity.addPart("condition", new StringBody(String.valueOf(ins[i].condition)));
} catch (IOException e) {
Log.e("MMP","Error in adding inspection data: "+e.getMessage());
}

try {
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
String result = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
Log.e("MMP","Error in handling result: "+e.getMessage());
}



publishProgress(i+1,ins.length);
}
return null;
}

最佳答案

如果是 uri 字符串,则需要文件名。谢谢@mh

ContentResolver cr = mContext.getContentResolver();
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor cur = cr.query(Uri.parse(ins[i].image), projection, null, null, null);
if(cur != null) {
cur.moveToFirst();
String filePath = cur.getString(0);
File imageFile = new File(filePath);
if(imageFile.exists()) {
// do something if it exists
entity.addPart("image", new FileBody(imageFile));
}
else {
// File was not found
Log.e("MMP","Image not Found");
}
}
else {
// content Uri was invalid or some other error occurred
Log.e("MMP","Invalid content or some error occured");
}

关于android - 打开失败 : ENOENT (No such file or directory) when uploading an image to a website using POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13598389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com