Show image brought from a database in Android Studio

1

We want to show an image, previously saved in a database and shown in the android layout but we do not know how to do it. At the moment we have this:

AccesoPHP aPHP = new AccesoPHP();

        is = aPHP.ejecutar("http://"+IP+"/imagen.php");

        Scanner sc = new Scanner(is);

        String ima = null;


        URL imageUrl = null;
        HttpURLConnection conn = null;

        ImageView img = findViewById(R.id.img);


        while (sc.hasNext()) {
            Toast.makeText(this, sc.next(), Toast.LENGTH_SHORT).show();
            try {
                Toast.makeText(this, ima, Toast.LENGTH_SHORT).show();
                imageUrl = new URL(ima);
                conn = (HttpURLConnection) imageUrl.openConnection();
                conn.connect();

                Bitmap imagen = BitmapFactory.decodeStream(conn.getInputStream());
                img.setImageBitmap(imagen);


            } catch (IOException e) {

                e.printStackTrace();

            }
            sc.close();

        }
    
asked by Alvaro Ruiz 08.06.2018 в 18:04
source

1 answer

1

ok if you get the url of the image:

String is = aPHP.ejecutar("http://"+IP+"/imagen.php");

You can use several methods, for example a Asynctask :

   class LoadImage extends AsyncTask<String, Void, Bitmap> {

        private final WeakReference<ImageView> imageViewReference;

        public LoadImage(ImageView imageView) {
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            try {
                return downloadBitmap(params[0]);
            } catch (Exception e) {
                Log.e("LoadImage class", "doInBackground() " + e.getMessage());
            }
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (isCancelled()) {
                bitmap = null;
            }

            if (imageViewReference != null) {
                ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    if (bitmap != null) {
                        imageView.setImageBitmap(bitmap);
                    }
                }
            }
        }

        private Bitmap downloadBitmap(String url) {
            HttpURLConnection urlConnection = null;
            try {
                URL uri = new URL(url);
                urlConnection = (HttpURLConnection) uri.openConnection();
                int statusCode = urlConnection.getResponseCode();
                if (statusCode != HttpURLConnection.HTTP_OK) {
                    return null;
                }

                InputStream inputStream = urlConnection.getInputStream();
                if (inputStream != null) {
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    return bitmap;
                }
            } catch (Exception e) {
                urlConnection.disconnect();
                Log.e("LoadImage class", "Descargando imagen desde url: " + url);
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }
            return null;
        }
    }

that you would use in this way, obtaining the instance of ImageView where you will add the image and send the url of the image so that the bitmap is obtained and added to the ImageView .

String is = aPHP.ejecutar("http://"+IP+"/imagen.php");

ImageView img = findViewById(R.id.img);
 new LoadImage(img).execute(is);

Other options are the use of Glide or Picasso .

    
answered by 08.06.2018 в 18:38