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