vectoriales vectorial una studio ruta obtener mostrar mapa imagenes imagen galeria entre diferencia desde cuál cargar android listview imageview simpleadapter

android - vectorial - Mostrar imagen de mapa de bits en la vista de imagen con un adaptador simple



obtener la ruta de una imagen android studio (3)

Básicamente, el adaptador simple vincula automáticamente algún ID de recurso o URI a la vista de la imagen del diseño de la fila. Pero no es compatible con Bitmap.

Eso es un problema, porque todos los que tenían que administrar el mapa de bits saben que a menudo tenemos que reducir el tamaño de la imagen para evitar excepciones de OutOfMemory. Pero si desea agregar imágenes a un listView, no puede reducir el tamaño de la imagen si solo proporciona URI. Así que aquí está la solución:

He modificado el Adaptador simple para poder manejar mapa de bits. Agregue esta clase a su proyecto y úselo en lugar de simpleAdapter. Luego, en lugar de pasar un URI o un ressourceId por una imagen, ¡pase un mapa de bits!

A continuación se encuentra el código:

import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.Context; import android.graphics.Bitmap; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Checkable; import android.widget.ImageView; import android.widget.SimpleAdapter; import android.widget.TextView; public class ExtendedSimpleAdapter extends SimpleAdapter{ List<? extends Map<String, ?>> map; // if fails to compile, replace with List<HashMap<String, Object>> map String[] from; int layout; int[] to; Context context; LayoutInflater mInflater; public ExtendedSimpleAdapter(Context context, List<? extends Map<String, ?>> data, // if fails to compile, do the same replacement as above on this line int resource, String[] from, int[] to) { super(context, data, resource, from, to); layout = resource; map = data; this.from = from; this.to = to; this.context = context; } @Override public View getView(int position, View convertView, ViewGroup parent) { mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); return this.createViewFromResource(position, convertView, parent, layout); } private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource) { View v; if (convertView == null) { v = mInflater.inflate(resource, parent, false); } else { v = convertView; } this.bindView(position, v); return v; } private void bindView(int position, View view) { final Map dataSet = map.get(position); if (dataSet == null) { return; } final ViewBinder binder = super.getViewBinder(); final int count = to.length; for (int i = 0; i < count; i++) { final View v = view.findViewById(to[i]); if (v != null) { final Object data = dataSet.get(from[i]); String text = data == null ? "" : data.toString(); if (text == null) { text = ""; } boolean bound = false; if (binder != null) { bound = binder.setViewValue(v, data, text); } if (!bound) { if (v instanceof Checkable) { if (data instanceof Boolean) { ((Checkable) v).setChecked((Boolean) data); } else if (v instanceof TextView) { // Note: keep the instanceof TextView check at the bottom of these // ifs since a lot of views are TextViews (e.g. CheckBoxes). setViewText((TextView) v, text); } else { throw new IllegalStateException(v.getClass().getName() + " should be bound to a Boolean, not a " + (data == null ? "<unknown type>" : data.getClass())); } } else if (v instanceof TextView) { // Note: keep the instanceof TextView check at the bottom of these // ifs since a lot of views are TextViews (e.g. CheckBoxes). setViewText((TextView) v, text); } else if (v instanceof ImageView) { if (data instanceof Integer) { setViewImage((ImageView) v, (Integer) data); } else if (data instanceof Bitmap){ setViewImage((ImageView) v, (Bitmap)data); } else { setViewImage((ImageView) v, text); } } else { throw new IllegalStateException(v.getClass().getName() + " is not a " + " view that can be bounds by this SimpleAdapter"); } } } } } private void setViewImage(ImageView v, Bitmap bmp){ v.setImageBitmap(bmp); } }

Esta clase se comporta exactamente como la clase original (SimpleAdapter)

Obtengo una imagen de una url. Estoy usando imageview en listview. Quiero agregar la lista de imágenes de mapa de bits en cada fila del elemento de la lista. Usé SimpleAdapter pero la imagen muestra en blanco. ¡Mi código está debajo!

ArrayList<HashMap<String, Bitmap>> mylist = new ArrayList<HashMap<String, Bitmap>>(); Bundle bundle = this.getIntent().getExtras(); get = bundle.getString("name"); try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.propertyhookup.com/mobile/propertylist.php"); nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("zipcode", get.trim())); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); }catch(Exception e){ Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); } //convert response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "/n"); } is.close(); result=sb.toString(); }catch(Exception e){ Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); } if(result.length()<= 7){ Toast.makeText(getApplicationContext(), "No properties for this zipcode or check your zipcode ", Toast.LENGTH_LONG).show(); //text.setText("No properties for this zipcode or check your zipcode"); } else{ try{ jArray = new JSONObject(result); }catch(JSONException e){ Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); } //JSONObject json = JSONfunctions.getJSONfromURL("http://192.168.1.111/propertyhookup.com/mobile/propertylist.php"); try{ JSONArray earthquakes = jArray.getJSONArray("earthquakes"); for(int i=0;i<10;i++){ map = new HashMap<String, Bitmap>(); //HashMap<String, Drawable> map1 = new HashMap<String, Drawable>(); JSONObject e = earthquakes.getJSONObject(i); if(e.getString("property_type").contains("1")) { proptype ="Single Family Home"; }else if(e.getString("property_type").contains("2")) { proptype="Condo"; }else if(e.getString("property_type").contains("3")) { proptype="Townhouse"; } if(e.getString("estimated_price").contains("0")) { estimate = "Not Enough Market Value"; //estimat = (TextView) findViewById(R.id.estimat); //estimat.setTextColor(Color.rgb(0, 0, 23)); }else { estimate = "$"+e.getString("estimated_price"); } photo = e.getString("photo1"); drawable = LoadImageFromWebOperations(photo); //text.setImageDrawable(d); try { aURL = new URL(photo); } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } URLConnection conn = null; try { conn = aURL.openConnection(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { conn.connect(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } InputStream is = null; try { is = conn.getInputStream(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } BufferedInputStream bis = new BufferedInputStream(is,8*1024); Bitmap bm = BitmapFactory.decodeStream(bis); map.put(photos, bm); mylist.add(map); } }catch(JSONException e) { Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show(); } SimpleAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main4, new String[] { "percent","propertyid", "cityname", "statecode", "propertytype", "footage", "bathroom", "bedroom", "price", "estimated", "photos" }, new int[] { R.id.percent, R.id.property_id, R.id.city_name, R.id.state_code, R.id.prop_type, R.id.foot, R.id.bath, R.id.bed, R.id.list, R.id.estimat, R.id.image}); setListAdapter(adapter);


Creo que es porque está descargando la imagen desde la web y necesita hacer esto en ASYNC para ver la imagen de descarga sin dolor y luego actualizar las imágenes.


La mejor manera de hacerlo es crear una clase que extienda BaseAdapter y luego instanciar una tarea asíncrona para cada imagen (en post ejecutar, establecer el mapa de bits en el correpondent imageView). Aquí hay una función simple para descargar una imagen de la web:

private Bitmap loadImageFromNetwork(String url) throws MalformedURLException, IOException { HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection(); conn.connect(); return BitmapFactory.decodeStream(new FlushedInputStream(conn.getInputStream())); }