2012-11-28 29 views
2

我將圖像的Uri字符串作爲字符串保存到SQLite。我可以通過創建視圖來查看圖像。打開失敗:使用POST將圖像上傳到網站時ENOENT(沒有這樣的文件或目錄)

但是當我把圖片上傳到使用FileBody Web服務器(新文件(theUriFromTheDatabase)),它總是說:「打開失敗:ENOENT(沒有這樣的文件或目錄)」

該文件的URI是:「/內容:/媒體/外部/圖像/媒體/ 667"

事實:

  1. 我敢肯定的文件是存在的,因爲我可以看到它
  2. 我能讀/寫內部存儲,外部存儲讀寫權限
  3. 使用的Galaxy Tab 2 10.1
  4. 它不會有一個SD卡
  5. 相同的代碼工作在我的Experia還新V的SD卡(是不是因爲它沒有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; 
    } 
+0

你嘗試通過在URI爲'URI'對象到'FileBody'的文件參數,即:'新文件(新的URI(theUriFromTheDatabase))'?或者,使用uri查詢'ContentResolver'並從返回的遊標中獲取實際的文件位置,如[演示文稿](http://stackoverflow.com/a/9989900/1029225)。 –

+0

我做了這樣的文件(Uri.parse(fromDb)),但它說構造函數沒有定義。 – thedjaney

+0

這是因爲'Uri'和'URI'是兩個不同的東西(注意大寫字母的區別)。只有一個'File'構造函數需要後者。換句話說:將數據庫中的字符串解析爲** ['URI'](http://developer.android.com/reference/java/net/URI.html)**而不是'Uri'。 –

回答

3

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

我有一個從用戶剛剛選擇的圖像文件的URI。 我該怎麼做你在這裏所做的,我不理解代碼。 – Sohaib

+2

我認爲這是另一篇文章:http://stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore – thedjaney

相關問題