-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);
}
}
}
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?
– Ricardo Pontual
I put it up there bro
– TurmaB ciccopn
Use the Glide or Icasso library
– Murillo Comino
How do I enter my Murilo code ?
– TurmaB ciccopn
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?
– Peter
I’ll put the php file code up, Peter
– TurmaB ciccopn
Ah OK! In the BDD you have the image URL and you want to show the image on Android? That?
– Peter