我將圖像的Uri字符串作爲字符串保存到SQLite。我可以通過創建視圖來查看圖像。打開失敗:使用POST將圖像上傳到網站時ENOENT(沒有這樣的文件或目錄)
但是當我把圖片上傳到使用FileBody Web服務器(新文件(theUriFromTheDatabase)),它總是說:「打開失敗:ENOENT(沒有這樣的文件或目錄)」
該文件的URI是:「/內容:/媒體/外部/圖像/媒體/ 667"
事實:
- 我敢肯定的文件是存在的,因爲我可以看到它
- 我能讀/寫內部存儲,外部存儲讀寫權限
- 使用的Galaxy Tab 2 10.1
- 它不會有一個SD卡
- 相同的代碼工作在我的Experia還新V的SD卡(是不是因爲它沒有SD卡?)
- 試圖消除啓動應用程序
- 拴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爲'URI'對象到'FileBody'的文件參數,即:'新文件(新的URI(theUriFromTheDatabase))'?或者,使用uri查詢'ContentResolver'並從返回的遊標中獲取實際的文件位置,如[演示文稿](http://stackoverflow.com/a/9989900/1029225)。 –
我做了這樣的文件(Uri.parse(fromDb)),但它說構造函數沒有定義。 – thedjaney
這是因爲'Uri'和'URI'是兩個不同的東西(注意大寫字母的區別)。只有一個'File'構造函數需要後者。換句話說:將數據庫中的字符串解析爲** ['URI'](http://developer.android.com/reference/java/net/URI.html)**而不是'Uri'。 –