Check for file on the web

Asked

Viewed 312 times

0

Next, I have an Android app, this app wants to check the existence of an image on the web to be able to load by 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 load and if there is no informing me so I can define a message. I think it is well explained. It should not be a picture, 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");

}

3 answers

3

Fabricio,

There are two ways to do this, I especially prefer the first. You do not need to do a "manual test" of the file’s existence, Picasso himself, if he cannot upload the image, will inform you that it was not possible to upload and this may mean that there was a failure on the internet, 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 the 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);

Observe the method onImageLoadFailed(), will be in it that you will include the code regarding the error message you commented you would like to display.

If you don’t want to use Builder, you can do it this way:

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.

2

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 status returned is equal to 200 means the file exists.

0

I’m going to guess that you have a function called calledFile() and it sets the boolean 'archiv_exists' to 'true' when you find it. Inside of conferengFile() you must make the request and pick up the answer, can be an httpRequest, if the return is 404, for example, you already know that there is nothing. Finally, there are other ways to check if the path has something, if it is an image, etc. All this logic must be within this conferenceFile(). Without making the call and analyzing it is impossible to know if the file is there or not. Usually the status returned = 200 means that the file exists, but it may still fail to load or not be an image (it may not be the case for your APP).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.