3
我想仙文件到服務器實際路徑,我需要從URI得到真正的路徑。機器人:從URI
我的代碼:
public String getPathFromURI(Context context, Uri contentUri) {
if (contentUri.toString().indexOf("file:///") > -1){
return contentUri.getPath();
}
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}finally {
if (cursor != null) {
cursor.close();
}
}
}
而且onActivityResult:
...
imageName = data.getData();
imagePath = getPathFromURI(getBaseContext(),imageName);
Picasso.with(getBaseContext()).load(imageName).into(imageView);
...
它怎麼可能是圖像顯示的ImageView,但ImagePath的始終是空? :)謝謝
編輯:
我如何發送圖像到服務器
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost([URL TO A SERVER]);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("uploaded_file_1", new FileBody(new File(imagePath)));
httpPost.setEntity(entity);
httpClient.execute(httpPost, localContext);
可以驗證真實路徑,你的代碼的一部分,是貝ng叫? 'getPathFromURI'方法中的'if'語句或'else'部分。另外,你可以記錄imageName(URI)的值來查看它是什麼。烏里 – NightwareSystems 2014-10-04 18:49:53
值是「內容://com.android.blah.blah」 – 2014-10-04 18:51:10
而且堪稱是第二 – 2014-10-04 18:51:55