Estoy desarrollando una aplicación en la cual, hago clic en la imagen usando la cámara y mostrar en una vista de cuadrícula. pero mi imagen no aparece en la vista de la imagen sin mostrar ningún error. pero cuando depurar el proyecto noto que, mi lista matriz está vacía. No entiendo exactamente donde mi código que va mal.
aquí está mi código:
/* private GridView gvGallery;*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK && null != data) {
bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
ArrayList<Contact> imageArry = new ArrayList<Contact>();
imageArry.add(new Contact(byteArray));
cameraAdapter = new CameraAdapter(this, R.layout.gv_item, imageArry);
gvGallery.setAdapter(cameraAdapter);
gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)
gvGallery.getLayoutParams();
mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);
Log.d("LOG_TAG", "Selected Images" + imageArry.size());
}
}
Mi adaptador de Clase:
private class CameraAdapter extends ArrayAdapter<Contact> {
Context context;
int layoutResourceId;
// BcardImage data[] = null;
ArrayList<Contact> data;
CameraAdapter(Context context, int layoutResourceId, ArrayList<Contact> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.ivGallery);
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
Contact picture = data.get(position);
//convert byte to bitmap take from contact class
byte[] outImage=picture._image;
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.imgIcon.setImageBitmap(theImage);
return row;
}
class ImageHolder
{ ImageView imgIcon; }
}
clase de contactos:
private static class Contact {
byte[] _image;
// Empty constructor
public Contact() { }
public Contact(byte[] _image) {
this._image = _image;
}
public byte[] get_image() {
return _image;
}
public void set_image(byte[] _image) {
this._image = _image;
}
}
y aquí está mi archivo XML para el botón y gridview:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_below="@+id/relativeLayout1"
android:layout_marginTop="90dp">
<Button
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="70dp"
android:text="@string/Camera"
android:backgroundTint="@android:color/white"
android:layout_alignParentStart="true"
android:textAllCaps="true" />
<Button
android:id="@+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Gallery"
android:layout_marginEnd="70dp"
android:backgroundTint="@android:color/white"
android:layout_alignParentEnd="true"
android:textAllCaps="true"
tools:ignore="NotSibling,UnknownId" />
</RelativeLayout>
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gv"
android:numColumns="3"
android:layout_weight="1">
</GridView>
</RelativeLayout>
**and another gv_item.XML file for imageview :**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/ivGallery"
android:layout_height="130dp"
android:layout_width="130dp"
android:padding="10dp"
android:src="@mipmap/ic_launcher_round"
android:scaleType="fitXY"
/>
</LinearLayout>
Como noto la matriz de imágenes está vacía por lo que sólo usted no está viendo cualquier imagen. Primero se debe agregar la imagen antes de pasar a la serie adaptador.
ArrayList<Contact> imageArry = new ArrayList<Contact>();
cameraAdapter = new CameraAdapter(this, R.layout.gv_item, imageArry);
Se puede ver desde arriba código imageArry está vacía y no contiene nada
Añadir datos a imageArry utilizando imageArray.add(Contact)
como se muestra a continuación
ArrayList<Contact> imageArry = new ArrayList<Contact>();
imageArray.add(new Contact(Add your data as per model class))
cameraAdapter = new CameraAdapter(this, R.layout.gv_item, imageArry);
Una vez que añadir que debe posiblemente ver los datos. Espero que esto ayude.