Search code examples
androidandroid-imageandroid-bitmap

Getting snapshot image from IP Camera via URL


I am trying to get a picture from a Hikvision IP Camera URL which has a a URL that is more or less formed like this:
http://IPaddress:port#/Streaming/channels/1/picture
However as you notice the folder is just named picture with no straight link to an image and its extensions. When I use Picasso library or just normal HttpConnection it fails to get the image / bitmap. How else can I retrieve the image? When I enter the URL in my web browser it loads perfectly and shows me that the picture is .jpeg format.

EDIT
Whenever I try to access the snapshot image using InputStream and Bitmap I get a FileNotFoundException error


Solution

  • The problem as pointed out by Dario was the authentication. Status 401 was being returned and that is why the FileNotFoundException was being thrown.
    The answer he gave me had a lot of deprecated methods my Gradle couldnt even get some of the classes.
    However I found another way with help from the following forums 1, 2 so I combined with another page for the authentication which I cant find and had the following:

    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");
             }
    



    Had to add the following line in the gradle under the android section:
    useLibrary 'org.apache.http.legacy'