使用GridView(ArrayAdapter)捕获图像组合

时间:2015-12-23 02:35:08

标签: android gridview android-camera android-arrayadapter onactivityresult

我的代码包含一个网格视图,其中包含多个图像作为单独的视图。

  1. 这些图像可以由相机拍摄,也可以由用户从图库中选择

  2. 每个视图都有一个动作,允许用户向其添加图像或从适配器中完全删除视图

  3. 在任何给定时间,可能会有' X'只有一个没有图像的视图(用户需要将图像加载到其上的空白视图)
  4. 每次用户成功将图像添加到视图/行时,我都会通过向适配器添加新项来实现此目的
  5. 为了让onActivityResult()GridViewAdapter结合使用,我在我的活动中定义了一个私有类,允许我从适配器中显示图像选择器对话框,以及要在主要活动中定义的onActivityResult()

    以下是我的整个代码

    public class ImageGridView extends Activity {
    
        GridView gridView;
        GridViewAdapter gridViewAdapter;
        ArrayList<GridItem> list;
        Uri mUri;
        String imageLocation;
        ImageView imageView;
        public static final int IMAGE_CAPTURE =1337, IMAGE_GALLERY=12;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_image_grid_view);
            gridView = (GridView) findViewById(R.id.gridview);
            //This image view is used as a test to make sure a bitmap is being returned from onActivityResult
            imageView=(ImageView)findViewById(R.id.image_original);
    
            list = new ArrayList<>();
            GridItem gridItem = new GridItem();
            list.add(gridItem);
            gridViewAdapter = new GridViewAdapter(ImageGridView.this, R.layout.row_gridview, list);
            gridView.setAdapter(gridViewAdapter);
        }
    
        private class GridViewAdapter extends ArrayAdapter<GridItem> {
    
            private Context mContext;
            private int layoutResourceId;
            private ArrayList<GridItem> mGridData = new ArrayList<GridItem>();
    
            public GridViewAdapter(Context mContext, int layoutResourceId, ArrayList<GridItem> mGridData) {
                super(mContext, layoutResourceId, mGridData);
                this.layoutResourceId = layoutResourceId;
                this.mContext = mContext;
                this.mGridData = mGridData;
            }
    
            public void setGridData(ArrayList<GridItem> mGridData) {
                this.mGridData = mGridData;
                notifyDataSetChanged();
            }
    
            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                View row = convertView;
                final ViewHolder holder;
    
                if (row == null) {
                    LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
                    row = inflater.inflate(layoutResourceId, parent, false);
                    holder = new ViewHolder();
                    holder.btnAdd = (ImageView) row.findViewById(R.id.imgAdd);
                    holder.btnRemove = (ImageView) row.findViewById(R.id.imgRemove);
                    holder.imageView=(ImageView)row.findViewById(R.id.imgPrescriptionPicasso);
                    row.setTag(holder);
                } else {
                    holder = (ViewHolder) row.getTag();
                }
                // NOT sure if this is the correct way to go about it.
                // How do I add Uri of image from onActivityResult to this
                if(mGridData[position].getImageLocationAsString()!=null) {
                   String imageLocation = mGridData[position].getImageLocationAsString();
                   holder.imageView.setImageBitmap(BitmapFactory.decodeFile(imageLocation);
                holder.btnAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        selectImage();
    
                    }
                });
    
                holder.btnRemove.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mGridData.remove(position);
                        notifyDataSetChanged();
                        //Remove this view entirely and notify adapter of the change
                    }
                });
                return row;
            }
             class ViewHolder {
                ImageView imageView;
                ImageView btnAdd, btnRemove;
                 //The buttons have been defined as image Views for now.
            }
        }
    
        private void selectImage() {
            final CharSequence[] items = {"Take Photo", "Choose from Gallery",
                    "Cancel"};
            AlertDialog.Builder builder = new AlertDialog.Builder(ImageGridView.this);
            builder.setTitle("Add picture of Prescription");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {
                    if (items[item].equals("Take Photo")) {
    
                        File f = getOutputMediaFile();
                        mUri = Uri.fromFile(f);
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
                        startActivityForResult(intent, IMAGE_CAPTURE);
    
                        Log.d("Clicked", "Take photo");
                    } else if (items[item].equals("Choose from Gallery")) {
    
                        Intent intent = new Intent(
                                Intent.ACTION_PICK,
                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        intent.setType("image/*");
                        startActivityForResult(
                                Intent.createChooser(intent, "Select File"),
                                IMAGE_GALLERY);
    
                    }
                }
            });
            builder.show();
        }
    
        private File getOutputMediaFile() {
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DCIM), "Medit");
            if (!mediaStorageDir.exists()) {
                mediaStorageDir.mkdirs();
            }
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File mediaFile;
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG" + timeStamp + ".jpg");
            Log.d("reached a", mediaFile.toString());
            return mediaFile;
        }
    
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data){
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode== Activity.RESULT_OK){
                switch(requestCode) {
                    case IMAGE_CAPTURE:
                        Bitmap bitmap= BitmapFactory.decodeFile(mUri.getPath());
                        imageView.setImageBitmap(bitmap);
                        //If user loads an image, a new view is created with a blank image
                        GridItem gridItem = new GridItem();
                        list.add(gridItem);
                        gridViewAdapter.notifyDataSetChanged();
                        break;
    
                    case IMAGE_GALLERY:
                        Uri selectedImageUri = data.getData();
                        String[] filePathColumn = { MediaStore.Images.Media.DATA };
                        Cursor cursor = getContentResolver().query(selectedImageUri,
                                filePathColumn, null, null, null);
                        cursor.moveToFirst();
                        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                        imageLocation = cursor.getString(columnIndex);
                        cursor.close();
                        Bitmap bitmap1= BitmapFactory.decodeFile(imageLocation);
                        imageView.setImageBitmap(bitmap1);
                        //If user loads an image, a new view is created with a blank image
                        GridItem gridItem1 = new GridItem();
                        list.add(gridItem1);
                        gridViewAdapter.notifyDataSetChanged();
                        break;
                }//Close switch statement
    
            }else {
                Log.d("Error ", "user canceled !");
            }
        }
    
    }
    

    我的imageView中定义的Activity正在onActivityResult()中设置,只是为了测试是否实际返回了位图。

    我需要帮助

    1. 在创建view holder中,如果用户设置了一个位图,则将位图设置为图像视图
    2. 我想到以某种方式在onActivityResult()代码本身设置特定网格项的图像。
    3. GridItem当前有一个我正在尝试使用的String,用于存储图像的图像位置(如果存在)并在getView() GridViewAdapter的{​​{1}}持有者函数中加载位图但是我被困在这里。

      这样做的最终目的是让用户向GridViewAdapter添加尽可能多的图像,最后通过意图将Uri.getPath()(imageLocation as String)发送到我将要使用的下一个活动一个数组。

      非常感谢我能为此获得的任何帮助/指导。感谢

0 个答案:

没有答案