我正试图从海康威视IP摄像机的URL中获取图片,该URL的格式大致如下:
http://IPaddress:port#/Streaming/channels/1/picture
然而,正如您所注意到的,文件夹只是命名为picture,没有直接链接到图像及其扩展名。当我使用Picasso库或只是普通的HttpConnection时,它无法获得图像/位图。否则我如何检索图像?当我在网络浏览器中输入URL时,它会完美加载,并显示图片为.jpeg格式
编辑
每当我尝试使用InputStream
和Bitmap
访问快照图像时,我都会收到一个FileNotFoundException
错误
Dario指出的问题是身份验证。返回状态401,这就是抛出FileNotFoundException
的原因
他给我的答案有很多不推荐的方法,我的Gradle甚至连一些类都拿不到
然而,在以下论坛1、2的帮助下,我找到了另一种方法,所以我结合了另一个我找不到的验证页面,并得到了以下内容:
boolean isSaved = false;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(AppConst.IMAGE_URL));
UsernamePasswordCredentials credentials =
new UsernamePasswordCredentials(AppConst.USERNAME, AppConst.PASSWORD);
BasicScheme scheme = new BasicScheme();
Header authorizationHeader = scheme.authenticate(credentials, request);
request.addHeader(authorizationHeader);
HttpResponse response = client.execute(request);
Log.d("responseType",response!=null? response.getEntity().getContentType().toString():"emptyType");
Log.d("responseCode", response != null ? String.valueOf(response.getStatusLine().getStatusCode()) : "emptyCode");
if((response.getStatusLine().getStatusCode()==200) && (response.getEntity()!=null)){//status OK and not empty
//save stuff
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String path = APP_DIR+System.currentTimeMillis()+".jpg";
FileOutputStream output = new FileOutputStream(path);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = instream.read(buffer)) != -1) {
output.write(buffer, 0, len);
}
output.close();
Log.d("Snapshot Filename: ", path);
isSaved = true;
}
}else{
isSaved= false;
}
}catch (Exception ex){
Log.e("saveImageError",ex!=null? ex.getMessage():"error");
}
必须在android
部分下的渐变中添加以下行:useLibrary 'org.apache.http.legacy'