且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用POST上传图片到网站时ENOENT(没有这样的文件或目录):打开失败

更新时间:2023-02-22 20:54:16

它需要的文件名,而不是如果URI字符串。感谢@mh

  ContentResolver的CR = mContext.getContentResolver();
的String [] =投影{MediaStore.MediaColumns.DATA};
光标CUR = cr.query(Uri.parse(INS [I]在图像配),投影,NULL,NULL,NULL);
如果(CUR!= NULL){
    cur.moveToFirst();
    串文件路径= cur.getString(0);
    文件镜像文件=新的文件(文件路径);
    如果(imageFile.exists()){
        //做一些事情,如果它存在
        entity.addPart(形象,新FileBody(镜像文件));
    }
    其他{
        //文件未找到
        Log.e(MMP,没有找到图片);
    }
}
其他{
    //开放的内容是无效的或发生其他错误
    Log.e(MMP,无效的内容或一些错误发生);
}

I saved the Uri String of an image to SQLite as string. I can view the image by creating a view intent for it.

but when I upload the image to the web server using FileBody(new File(theUriFromTheDatabase)) it always saying "open failed: ENOENT (No such file or directory)"

The file's uri is: "/content:/media/external/images/media/667"

Facts:

  1. I'm sure the file is there because I can view it
  2. I enabled internal storage read/write, external storage read/write permissions
  3. Using a Galaxy Tab 2 10.1
  4. It doesn't have an SD card
  5. Same code works on my Experia Neo V with SD card (is it because it has no SD card?)
  6. Tried removing the wire before starting the app
  7. Tethered USB is off

Here is the code:

    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;
    }

It needed the filename instead if the uri string. thanks @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");
}