2

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

Manny265
  • 1,709
  • 4
  • 23
  • 42
  • Try to use developer tools of your browser to see if that URL redirect to another url but even in this case you shouldn't receive FileNotFoundException. Are you sure that you access to the correct ip and port from your client? – Dario Picco Nov 15 '15 at 14:00
  • already did that and the URL is the same. Im so sure,I even copied and pasted it from my android studio project straight into the browser and it worked @DarioPicco – Manny265 Nov 15 '15 at 14:07
  • 1
    You use a real device to debug? if yes try to put the same url in the browser of the mobile device and check if the IP Camera use an authentication. Or if the IPCamera is public please paste a temporal link. PS: Have you added INTERNET in the manifest of your app? – Dario Picco Nov 15 '15 at 14:12
  • I am using real device,stream can get through via SurfaceView object but since I didnt know how to capture an image from this I decided to get straight from IP camera. It uses authentication and I have passed authentication details along. same url with authentication details pasted in browser works properly. @DarioPicco – Manny265 Nov 15 '15 at 14:24
  • Can't help without connecting to the IPCamera. Try to debug step by step for example try to connect with InputStream and download another image from internet, then try to connect with InputStream to your IPCamera and read the response, etc.. – Dario Picco Nov 15 '15 at 14:32
  • try this link,I will take down the temporary account in an hour http://so:so54321!@omafano.ddns.net:4321/Streaming/channels/1/picture – Manny265 Nov 15 '15 at 14:33
  • @DarioPicco check above. u see the name is just picture. if u try to append a file type it wont work – Manny265 Nov 15 '15 at 14:34
  • Ok I see. Yes "picture" is just a name, the extension is only a convention you obviously try to download "picture" and then you can rename it whatever you want for example picture.jpg if this match the content format – Dario Picco Nov 15 '15 at 14:37
  • I try to download using a real android device please wait – Dario Picco Nov 15 '15 at 14:42
  • when I try to do that that is when the FileNotFound exception is thrown @DarioPicco and I am totally puzzled thought that would work – Manny265 Nov 15 '15 at 14:44
  • 1
    I try with a real device and I not receive FileNotFount but status code 401 - Unauthorized. In what line you receive the FileNotFound exception? – Dario Picco Nov 15 '15 at 15:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95178/discussion-between-manny264-and-dario-picco). – Manny265 Nov 15 '15 at 15:16

1 Answers1

1

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'

Community
  • 1
  • 1
Manny265
  • 1,709
  • 4
  • 23
  • 42
  • hai we are getting the same issue and we tried your code but we get the same 401 error. Can you please help me... – Naresh Feb 05 '19 at 07:17
  • HttpResponse response = client.execute(request); we are getting the error in this line. And the status is not 200, response.getStatusLine().getStatusCode()==200. It's getting 401 error. – Naresh Feb 05 '19 at 07:20
  • HI, iam getting the arryindexoutofbound exception in android. – v teja Feb 06 '20 at 11:08
  • @gowthami can you be more specific with your problem, what have you tried and not tried, where is the exception being thrown, what camera model and firmware version are you using? – Manny265 Feb 06 '20 at 18:49
  • @iOS as mentioned, status code 401 is authentication problems. What was your work around? – Manny265 Jan 28 '21 at 04:13