How can I view BD (Mysql) images(url) in Android Studio

Asked

Viewed 109 times

-1

How can I show images (url) of the Database (Mysql) in Android Studio?

Mainactivity.java

package com.example.APP;

import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    String urladdress="http://3m22p.dx.am/app_conexao.php";
    String[] nome;
    String[] comentarios;
    String[] imagem;
    ListView ListView;
    BufferedInputStream is;
    String line = null;
    String result = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView=(ListView)findViewById(R.id.lview);

        StrictMode.setThreadPolicy((new StrictMode.ThreadPolicy.Builder().permitNetwork().build()));
        collectData();
        CustomListView customListView = new CustomListView(this,nome,comentarios,imagem);
        ListView.setAdapter(customListView);
    }
    private void collectData()
    {
        //conexão
        try{
            URL url = new URL(urladdress);
            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            con.setRequestMethod("GET");
            is = new BufferedInputStream(con.getInputStream());
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        //Conteudo

        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine())!=null){
                sb.append(line+"\n");
            }
            is.close();
            result = sb.toString();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        //JSON

        try{
            JSONArray ja = new JSONArray(result);
            JSONObject jo = null;
            nome = new String[ja.length()];
            comentarios = new String[ja.length()];
            imagem = new String[ja.length()];

            for (int i=0;i<=ja.length();i++){
                jo = ja.getJSONObject(i);
                nome[i] = jo.getString("nome");
                comentarios[i] = jo.getString("comentarios");
                imagem[i] = jo.getString("imagens");
            }
        }
        catch(Exception ex) {

            ex.printStackTrace();
        }
    }
}

Customlistview.java

package com.example.APP;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.InputStream;


public class CustomListView extends ArrayAdapter <String>{

    private  String[] nomes_col;
    private  String[] comentarios;
    private  String[] imagem;
    private Activity context;
    Bitmap bitmap;

    public CustomListView(Activity context,String[] nomes_col,String[] comentarios,String[] imagem) {
        super(context, R.layout.layout,nomes_col);
        this.context=context;
        this.nomes_col=nomes_col;
        this.comentarios=comentarios;
        this.imagem=imagem;
    }

    @NonNull
    @Override

    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){

        View r = convertView;
        ViewHolder viewHolder = null;
        if (r==null){
            LayoutInflater layoutInflater = context.getLayoutInflater();
            r = layoutInflater.inflate(R.layout.layout,null,true);
            viewHolder = new ViewHolder(r);
            r.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolder)r.getTag();
        }

        viewHolder.tvw1.setText(nomes_col[position]);
        viewHolder.tvw2.setText(comentarios[position]);
        new GetImageFromURL(viewHolder.ivw).execute(imagem[position]);

        return r;
    }
    class ViewHolder{
        TextView tvw1;
        TextView tvw2;
        ImageView ivw;

        ViewHolder(View v){
            tvw1 = (TextView)v.findViewById(R.id.tvprofilename);
            tvw2 = (TextView)v.findViewById(R.id.tvcomentarios);
            ivw = (ImageView)v.findViewById(R.id.ImageView);
        }
    }
    public class GetImageFromURL extends AsyncTask<String,Void, Bitmap>{

        ImageView imgView;
        public GetImageFromURL(ImageView imgv){
            this.imgView = imgv;
        }
        @Override
        protected Bitmap doInBackground(String... url) {
            String urldisplay = url[0];
            bitmap = null;

            try{
                InputStream ist = new java.net.URL(urldisplay).openStream();
                bitmap = BitmapFactory.decodeStream(ist);
            }
            catch(Exception ex){
                ex.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap){

            super.onPostExecute(bitmap);
            imgView.setImageBitmap(bitmap);
        }
    }
}

BD in Mysql inserir a descrição da imagem aqui

File php: app_conexao.php inserir a descrição da imagem aqui

  • your question is too wide, can’t narrow it down to a more specific problem? can you share the code of what you’ve already done?

  • I put it up there bro

  • Use the Glide or Icasso library

  • How do I enter my Murilo code ?

  • From the moment where, in its code has String urladdress="http://3m22p.dx.am/app_conexao.php"; that seems to mean that the database is on a Web server, in other words, not on Android. In this case, it is not an Android problem but a PHP problem. Who makes the app_connection code.php? Vc?

  • I’ll put the php file code up, Peter

  • Ah OK! In the BDD you have the image URL and you want to show the image on Android? That?

Show 2 more comments

2 answers

1


To see on android an image when vc has URL:

 URL url_img = new URL("http://meu_servido.com/image.jpg");
 Bitmap data_bmp = BitmapFactory.decodeStream(url_img.openConnection().getInputStream());
 imageView.setImageBitmap(data_bmp);

It is sufficient to change the name of the URL according to the value in the database. For example if in BDD the image has only the name (eg "home") just "paste" all together type url_img = "http://me_servor.com/"+name_BDD+". jpg"; In the case of the "house" image, it will give http://meu_servidor.com/casa.jpg, in case of "bike" will give http://meu_servidor.com/bicicleta.jpg etc....

  • But I don’t want a specific image, I want each name to have its own image, which was placed (through a link) in the comic book

  • I modified the answer. Then, it depends on what you have in your base. Know if you have the full URL, only a piece etc...

0

Use library to do this, make it easy.

First add the library in build.Radle(app):

implementation group: 'com.github.bumptech.glide', name: 'glide', version: '4.9.0'

Inside your Adapter instead of using:

new GetImageFromURL(viewHolder.ivw).execute(imagem[position]);

use:

    //método da biblioteca para carregar as imagens
        Glide.with(context)
                .load(imagem[position])
                .apply(new RequestOptions()
                        .override(90, 90)). //aqui você define altura e largura em px
                into(viewHolder.ivw);

This way you can delete the method you created from asynctask

  • Okay thanks I’ll try that

  • 1

    You’re the greatest, oh bro IT WORKS! Thank you so much.

  • @Turmabciccopn If you can mark my answer as useful, help other users.

Browser other questions tagged

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