showing camera capture image to gridview

蹲街弑〆低调 提交于 2020-04-14 07:35:51

问题


I am developing an app in which, i click image using camera and show it into an grid view. but my image not showing in image view without showing any error. but when i debug the project i notice that, my array list is empty. i don't understand exactly where my code going wrong.

here is my code :

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

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

}

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

and here is my xml file for button and 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>

回答1:


As I notice your image array is empty so only you are not seeing any image. First add the image before passing array to adapter.

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

You can see from above code imageArry is empty and does not contain anything

Add data to imageArry using imageArray.add(Contact) like shown below

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

Once you add you should possibly see the data. Hope this helps.




回答2:


I will slightly change the answer Keshav1234. change adapter constructor like this:

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

and after you get the object Contact, call this method inside your activity/fragment

adapter.setContact(Contact);
adapter.notifyDataSetChanged();

As a result, your list will increase



来源:https://stackoverflow.com/questions/60985137/showing-camera-capture-image-to-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!