Verify existence of file on the web

0

Next, I have an Android app, this app wants to verify the existence of a web image to be able to load Picasso, if the image does not exist in an example address www.algumacoisa.com/img/imagem5.png I will show a message . So what I need is to validate the image (file) before loading, or try to upload and if it does not exist let me know so I can define a message. I think it's well explained. It should not necessarily be an image, or using Picasso.

Code:

Check if there is a file at www.lojaimpacto.com.br/img/foto05.png

if(arquivo existe){

        iv_xml01_logo = (ImageView) findViewById(R.id.iv_xml01_logo);
        Picasso.with(lista_fones.this)
                .load(www.lojaimpacto.com.br/img/foto05.png)
                .resize(300, 155)
                .centerInside()
                .into(iv_xml01_logo);

} else {

 System.out.println("Arquivo não existe");

}
    
asked by anonymous 12.05.2017 / 14:36

3 answers

3

Fabricio,

There are two ways to do this, I especially prefer the first one. You do not have to do a manual "test" of the existence of the file, Picasso himself, if he can not load the image, he will inform you that he could not load and that could mean that the internet has failed, or that the file does not exist. If there is no distinction between these types of errors for you do as demonstrated in the code below.

First, this can be done through Builder:

Picasso.Builder builder = new Picasso.Builder(this);
    builder.listener(new Picasso.Listener()
    {
        @Override
        public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception)
        {
            // MENSAGEM AQUI!
        }
    });
    builder.build().load(URL).into(imageView);

Note the onImageLoadFailed() method, which will include the code about the error message you commented on that you would like to display.

If you do not want to use Builder, you can do the following:

Picasso.with(mContext).load(fileImage)
                .into(holder.mImageEvidence, new Callback() {
                    @Override
                    public void onSuccess() {
                       // OK! ARQUIVO ENCONTRADO E CARREGADO!
                    }

                    @Override
                    public void onError() {
                        // MENSAGEM AQUI!
                    }
                });

Any questions, leave a comment to clarify.

    
12.05.2017 / 14:56
1
import java.net.*;
import java.io.*;

public static boolean exists(String URLName){
    try {
      HttpURLConnection.setFollowRedirects(false);
      HttpURLConnection con =
         (HttpURLConnection) new URL(URLName).openConnection();
      con.setRequestMethod("HEAD");
      return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    }
    catch (Exception e) {
       e.printStackTrace();
       return false;
    }
  }

If the returned status is equal to 200 means that the file exists.

    
12.05.2017 / 14:52
0

I'm going to kick you into having a function that calls confereFile () and it sets the boolean 'file_exist' to 'true' when you find it. Inside confereFile () you must make the request and get the response, it can be an httpRequest, if the return is 404, for example, you already know that there is nothing. Anyway, there are other ways to check if the path has something, if it is an image, etc. All this logic must be within this confereFile (). Without making the call and analyzing it it is impossible to know if the file is there or not. Usually the status returned = 200 means the file exists, but it can still fail to load or not be an image (this may not be the case for your APP).

    
12.05.2017 / 15:00