显示相机捕获的图像到gridview [英] showing camera capture image to gridview
问题描述
我正在开发一个应用程序,其中,我使用相机单击图像并将其显示为网格视图。但是我的图像没有显示在图像视图中,而没有显示任何错误。但是当我调试项目时,我注意到,我的数组列表为空。我不知道我的代码到底出了什么问题。
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.
这是我的代码:
/* 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;
}
}
这是我按钮的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>
推荐答案
我注意到您的图像数组为空,所以只有您没有看到任何图像。
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);
您可以从上面的代码中看到imageArry为空且不包含任何内容
You can see from above code imageArry is empty and does not contain anything
使用 imageArray.add(Contact)
向imageArry添加数据,如下所示
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.
这篇关于显示相机捕获的图像到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!