使用POST上传图片到网站时ENOENT

使用POST上传图片到网站时ENOENT

本文介绍了使用POST上传图片到网站时ENOENT(没有这样的文件或目录):打开失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保存的图像的URI字符串来SQLite作为字符串。我可以为它创造一个视图的意图查看图像。

但是当我把图片上传到使用FileBody Web服务器(新文件(theUriFromTheDatabase)),它总是说:打开失败:ENOENT(没有这样的文件或目录)

该文件的URI是:/内容:/媒体/外部/图像/媒体/ 667

事实:


  1. 我敢肯定的文件是存在的,因为我可以查看

  2. 我启用了读/写内部存储器,外部存储器读/写权限

  3. 使用的Galaxy Tab 2 10.1

  4. 它不具备一个SD卡

  5. 同一code工作在我的Experia还新V配合SD卡(是不是因为它没有SD卡?)

  6. 试图启动应用程序之前删除线

  7. 栓USB处于关闭状态

下面是code:

 保护无效doInBackground(虚空...... PARAMS){
        // TODO自动生成方法存根        InspectionsDbController分贝=新InspectionsDbController(getActivity());        InspectionItemStruct [] =插件db.getInspectionList(((所有MyApplication)((活动)mContext).getApplication())getCurrentInspectionId());        共享preferences设置= mContext.getShared preferences(MainActivity preFS_NAME,0);
        长的用户id = settings.getLong(用户id,0);
        字符串标记= settings.getString(令牌,);        的for(int i = 0; I< ins.length;我++){
            HttpClient的HttpClient的=新DefaultHttpClient();
            HttpContext的localContext =新BasicHttpContext();
            HttpPost httpPost =新HttpPost(http://webprojectupdates.com/mmp/api/mobile/upload_inspection);
                MultipartEntity实体=新MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);                //添加用户id
                尝试{
                    entity.addPart(用户id,新StringBody(将String.valueOf(用户ID)));
                    entity.addPart(令牌,新StringBody(将String.valueOf(令牌)));                }赶上(IOException异常五){
                    Log.e(MMP,错误添加标记:+ e.getMessage());
                }
                //添加媒体附件
                如果(INS [I]在图像配!= NULL){
                    //位图图像= BitmapFactory.de codeFILE(INS [I]在图像配);                    // ByteArrayOutputStream BOS =新ByteArrayOutputStream();
                    //image.com$p$pss(Com$p$pssFormat.JPEG,75,BOS);
                    //字节[]为imageData = bos.toByteArray();
                    // ByteArrayBody BAB =新ByteArrayBody(为imageData,图像/ JPG,插件[I] .itemId +JPG);                    //entity.addPart(\"image,BAB);
                    entity.addPart(形象,新FileBody(新文件(INS [I]在图像配)));
                }
                如果(INS [I]。视频!= NULL){
                    entity.addPart(视频,新FileBody(新文件(INS [I]。视频)));
                }                //普通字符串数据
                尝试{
                    entity.addPart(的itemId,新StringBody(将String.valueOf(INS [I] .itemId)));
                    entity.addPart(ISEXIST,新StringBody(将String.valueOf(INS [I] .itemExists)));
                    如果(INS [I]。点评!= NULL)entity.addPart(意见,新的StringBody(将String.valueOf(INS [I]。点评)));
                    entity.addPart(条件,新StringBody(将String.valueOf(INS [I] .condition)));
                }赶上(IOException异常五){
                    Log.e(MMP,在加入检查数据错误:+ e.getMessage());
                }                尝试{
                    httpPost.setEntity(实体);
                    HTT presponse响应= httpClient.execute(httpPost,localContext);
                    字符串结果= EntityUtils.toString(response.getEntity());
                }赶上(IOException异常五){
                    Log.e(MMP,在处理结果错误:+ e.getMessage());
                }            publishProgress第(i + 1,ins.length);
        }
        返回null;
    }


解决方案

它需要的文件名,而不是如果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");
}

这篇关于使用POST上传图片到网站时ENOENT(没有这样的文件或目录):打开失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 14:59