Error loading image in application by Json request

Asked

Viewed 35 times

1

I am developing an application that captures pictures sends to the server, the server saved in the database, and when the user wants, he can download these photos again on his mobile phone.

Even the save part in the database is fine. The problem is in converting the String URL of the database to image file.

I’m using spring framework to develop the API.

@RestController
@RequestMapping(value = "/api")
public class Services {

@Autowired
private PacienteRepository pr;

@GetMapping("/pacientes")
public List<Paciente> listaPaciente() {
    return pr.findAll();
}
}

Mobile app:

public static final String TAG = "LOG";
public static final String URL = "http://192.168.1.15:8080/api/pacientes";
private List<Paciente> pacientes;
private ListView lvPalheta;
private CustomAdapter adapter;
private ProgressDialog progressDialog;
private ImageView iv_capa;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_galery );
    lvPalheta = findViewById(R.id.listview_pacientes);
    iv_capa = findViewById(R.id.tv_qtd);
    listarPacientes();
}
protected void listarPacientes() {
    progressDialog = ProgressDialog.show(Galery.this, "Aguarde um momento", "Carregando pedidos ...", true, false);
    final StringRequest stringRequest = new StringRequest( Request.Method.GET, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            progressDialog.dismiss();
            Gson gson = new Gson();
            Type usuariosListType = new TypeToken<ArrayList<Paciente>>(){}.getType();
            pacientes = gson.fromJson(response, usuariosListType);
            CustomAdapter customAdapter = new CustomAdapter( getApplicationContext(), pacientes);
            lvPalheta.setAdapter(customAdapter);
            lvPalheta.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                }
            });



        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            progressDialog.dismiss();
            Toast.makeText( Galery.this, error.toString(), Toast.LENGTH_LONG ).show();
        }
    }){

    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}


class CustomAdapter extends BaseAdapter {

    private List<Paciente> pacientes;
    Context context;
    public CustomAdapter(Context context, List<Paciente> pacientes) {
        this.context = context;
        this.pacientes = pacientes;
    }

    @Override
    public int getCount() {
        return pacientes.size();
    }

    @Override
    public Object getItem(int i) {
        return pacientes.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {


        View v = getLayoutInflater().inflate(R.layout.card_view_pacientes, null);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.person);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        imageBytes = Base64.decode(imageString, Base64.DEFAULT);
        Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
        iv_capa.setImageBitmap(decodedImage);     
        return v;
    }

}

In the output it says that the conversion is returning a null Bitmap: inserir a descrição da imagem aqui

No answers

Browser other questions tagged

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