Send an image using JSON in node.js (express) to an android device

0

I am trying to send an image to the client using a server in node.js. When I type the URL in a browser I take the image. However, when I want to get it on my cell phone, it does not appear.

This is the code to send the url of the image:

app.use(express.static('images'));
app.post('/Imagen',function(req, res){
    var dir= "http://10.0.0.23:8080/basket.png";
    var obj={"img":dir};
    res.json(obj);
});

This code, when receiving a String with the JSON (which works), sends it to a class called LeerConsulta which reads the JSON object and obtains the image:

public class Accedido extends AppCompatActivity {

    ImageView imagen;
    Button ok;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accedido);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        imagen=(ImageView)findViewById(R.id.Img);
        ok=(Button)findViewById(R.id.btnOk);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bitmap img=null;
                Ajax im=new Ajax();
                try {
                    String imagen = im.execute(MainActivity.IP_ADDRESS + "Imagen").get();
                    Toast.makeText(Accedido.this, imagen, Toast.LENGTH_LONG).show();
                    LeerConsulta lec = new LeerConsulta(imagen, "img");
                }
                catch(Exception exc) {
                    Toast.makeText(Accedido.this, exc.toString(), Toast.LENGTH_SHORT).show();
                }
                imagen.setImageBitmap(img);
            }
        });
    }

Finally, this is the code that reads the JSON object and receives the image:

public class LeerConsulta{

    static String content="", data="";

    public LeerConsulta(String Content, String Dat)
    {
        content=Content;
        data=Dat;
    }
    protected Bitmap getBitMap()
    {
        try{
            JSONObject jsonResponse = new JSONObject(content);
            String src=jsonResponse.getString(data);
            java.net.URL url = new java.net.URL(src);
            HttpURLConnection con=(HttpURLConnection)url.openConnection();
            con.setDoInput(true);
            con.connect();
            InputStream input=con.getInputStream();
            Bitmap myBitmap=BitmapFactory.decodeStream(input);
            return myBitmap;
        }
        catch(Exception e){
            String err="EL error es este: ";
            Log.e(err,e.getMessage());
            //e.printStackTrace();
            return null;
        }
    }

}
    
asked by F. Riggio 09.01.2017 в 21:45
source

1 answer

1

Node.js is not like apache, where you can have public directories view the contents of the folders. When you make a static directory in Express, what you are doing is making the files available within.

  

Express looks for the files relative to the static directory, so the static directory name is not part of the URL.

When a request comes in, the routes that have been associated with the server are evaluated first; if it does not exist, proceed to search the static files.

If you want to keep the initial route, you must create a folder and have it be used, finally put the images folder in:

assets
|__images
   |__basket.png
   |__football.png
   |__tenis.png

And you serve this folder:

app.use(express.static(path.join(__dirname, 'assets')));

Now if you access http://10.0.0.23:8080/images/basket.png you will see that the image is served correctly.

    
answered by 09.01.2017 в 22:15