Java 向gridview显示摄像头捕获图像

Java 向gridview显示摄像头捕获图像,java,android,gridview,camera,bitmapimage,Java,Android,Gridview,Camera,Bitmapimage,我正在开发一个应用程序,在这个应用程序中,我使用相机单击图像并将其显示在网格视图中。但是我的图像没有显示在图像视图中,并且没有显示任何错误。但是当我调试项目时,我注意到,我的数组列表是空的。我不明白我的代码到底哪里出错了 这是我的代码: /* private GridView gvGallery;*/ protected void onActivityResult(int requestCode, int resultCode, Intent data) { // T

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

这是我的代码:

 /* 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());
            }
  }
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; }
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;
    }
}
 <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>
这是我的按钮和网格视图的xml文件:

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

**以及imageview的另一个gv_item.XML文件:**

我注意到您的图像数组为空,因此只有您没有看到任何图像。在将数组传递给适配器之前,首先添加映像

ArrayList<Contact> imageArry = new ArrayList<Contact>();
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);
}
//...
}

因此,您的列表将增加,显示在Main Activity中创建字段“contact”。您必须添加新的联系人对象好的。现在,我在网格视图中获得图像,但当我尝试添加新图像时,它会替换旧图像。如果我的回答对您有所帮助,请将其作为回答接受。您维护一个列出onActivityResult的列表。将列表初始化为类变量意味着ArrayList imageArry=new ArrayList();声明变量时,在类定义后添加此行。这将使您添加多个图像在添加此图像后,我的应用程序将崩溃而不显示任何错误:(o否)。我很抱歉。请删除适配器构造函数中的此行:“…super(上下文、layoutResourceId、数据);…”忘记删除此行。当然问题是this@droidbaza我也有同样的问题,我使用了你的代码,但没有任何东西可以解决我的问题