1
The code I have so far is as follows::
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Currency;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class NoticiasAdapter extends ArrayAdapter {
private final Activity activity;
private final List stocks;
// private final Image img;
public NoticiasAdapter(Activity activity, List objects) {
super(activity, R.layout.noticia_list_item , objects);
this.activity = activity;
this.stocks = objects;
// this.img = url;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
StockQuoteView sqView = null;
if(rowView == null)
{
// Get a new instance of the row layout view
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.noticia_list_item, null);
// Hold the view objects in an object,
// so they don't need to be re-fetched
sqView = new StockQuoteView();
//sqView.quote = (TextView) rowView.findViewById(R.id.ticker_symbol);
sqView.ticker = (TextView) rowView.findViewById(R.id.ticker_price);
sqView.img = (ImageView) rowView.findViewById(R.id.img);
// Cache the view objects in the tag,
// so they can be re-accessed later
rowView.setTag(sqView);
} else {
sqView = (StockQuoteView) rowView.getTag();
}
// Transfer the stock data from the data object
// to the view objects
Noticias currentStock = (Noticias) stocks.get(position);
String imagem3 = currentStock.getImagem();
sqView.ticker.setText(currentStock.getTitulo());
sqView.img.setImageURI(currentStock.getBitmapFromURL(imagem3));
// sqView.quote.setText(currentStock.getDescricao());
return rowView;
}
protected static class StockQuoteView {
protected TextView ticker;
protected TextView quote;
protected ImageView img;
}
}
XML code:
<TextView
android:id="@+id/ticker_price"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Example application"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="20sp" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/img" />
Remainder of the code:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
public class Noticias {
private String titulo;
private String link;
private String comentarios;
private String datapublicao;
private String criador;
private String categoria;
private String descricao;
private String imagem;
private String nomeimg = "imagem01";
public Noticias(String titulo, String link, String comentarios, String datapublicao ,String criador ,String categoria , String descricao,String imagem) {
this.titulo = titulo;
this.link = link;
this.comentarios = comentarios;
this.datapublicao = datapublicao;
this.criador = criador;
this.categoria = categoria;
this.descricao= descricao;
this.imagem = imagem;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public String getTitulo() {
return titulo;
}
public void setLink(String link) {
this.link = link;
}
public String getLink() {
return link;
}
public void setImagem(String imagem) {
this.imagem = imagem;
}
public String getImagem() {
return imagem;
}
public Uri getBitmapFromURL(String url) {
try {
URL src = new URL(url);
HttpURLConnection connection = (HttpURLConnection) src.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
salvando(myBitmap);
Uri imagemURI = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath() +
"caminho/imagem" + nomeimg );
return imagemURI;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private void salvando(Bitmap abmp){
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
nomeimg;
File dir = new File(file_path);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, nomeimg);
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
;
abmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
With this code you have, what he does so far?
– Lollipop
Before trying to show images, appeared the titles straight, after that the screen becomes all white.
– Gustavo
Everything appears blank, the name of the image I’m putting to be the image url, I don’t know if I can do this:public Uri getBitmapFromURL(String url) { Try { URL src = new URL(url); img = url; If the problem is the pictures pq the titles tbm do not appear?
– Gustavo