将相机捕获的图像显示到 gridview

showing camera capture image to gridview

我正在开发一个应用程序,我在其中使用相机单击图像并将其显示到网格视图中。但我的图像没有显示在图像视图中,没有显示任何错误。但是当我调试项目时我注意到,我的数组列表是空的。我不明白我的代码哪里出了问题。

这是我的代码:

 /* 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());
            }
  }

我的适配器Class:

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; }

}

联系方式class:

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;
    }
}

这是我的 xml 按钮和 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>

我注意到你的图像数组是空的,所以只有你没有看到任何图像。在将数组传递给适配器之前先添加图像。

ArrayList<Contact> imageArry = new ArrayList<Contact>();
cameraAdapter = new CameraAdapter(this, R.layout.gv_item, imageArry);

从上面的代码可以看出imageArry是空的,不包含任何东西

使用 imageArray.add(Contact) 向 imageArry 添加数据,如下所示

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);

添加后您应该可以看到数据。希望这有帮助。

我会稍微更改答案 Keshav1234。像这样更改适配器构造函数:

private class CameraAdapter extends ArrayAdapter<Contact> {
private Context context;
private int layoutResourceId;
private ArrayList<Contact> data = new ArrayList<>();

public CameraAdapter(Context context, int layoutResourceId) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
}

public void setData(Contact contact){
    data.add(contact);
}
//...
}

并且在您获得联系人对象后,在您的 activity/fragment

中调用此方法
adapter.setContact(Contact);
adapter.notifyDataSetChanged();

因此,您的列表会增加