如何动态添加多个imageview

时间:2016-12-21 07:38:31

标签: android imageview photo

我即将制作相册,这样我们就可以上传多张照片(如facebook等)

ivAttachment1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(strPhoto1 == "" && PageType.equals("NewRequestMedical")) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
                photoTemp = "photo1";

            }
            else if(strPhoto1 == "" && !PageType.equals("NewRequestMedical"))
            {
                Toast.makeText(getApplicationContext(), "there is no attachment on this request",
                        Toast.LENGTH_SHORT).show();
            }else {
                Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);

               /* i.putExtra("photo", strPhoto1);*/
                Bundle extras = new Bundle();
                extras.putString("photo", strPhoto1);
                extras.putString("photoNo","photo1");
                extras.putString("PageTypeRequest",PageType);
                i.putExtras(extras);
                startActivityForResult(i,100);
                //i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


            }
        }
    });

    ivAttachment2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(strPhoto2 == "" && PageType.equals("NewRequestMedical")) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
                photoTemp = "photo2";
            }
            else if(strPhoto2 == "" && !PageType.equals("NewRequestMedical"))
            {
                Toast.makeText(getApplicationContext(), "there is no attachment on this request",
                        Toast.LENGTH_SHORT).show();
            }
            else
            {
            Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);
           /* i.putExtra("photo", strPhoto2);
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);*/

                Bundle extras = new Bundle();
                extras.putString("photo", strPhoto2);
                extras.putString("photoNo","photo2");
                extras.putString("PageTypeRequest",PageType);
                i.putExtras(extras);
                startActivityForResult(i,100);
             }
        }
    });
    ivAttachment3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(strPhoto3 == "" && PageType.equals("NewRequestMedical")) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
                photoTemp = "photo3";
            }
            else if(strPhoto3 == "" && !PageType.equals("NewRequestMedical"))
            {
                Toast.makeText(getApplicationContext(), "there is no attachment on this request",
                        Toast.LENGTH_SHORT).show();
            }else
            {
                Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);
                Bundle extras = new Bundle();
                extras.putString("photo", strPhoto3);
                extras.putString("photoNo","photo3");
                extras.putString("PageTypeRequest",PageType);
                i.putExtras(extras);
                startActivityForResult(i,100);
                /*i.putExtra("photo", strPhoto3);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);*/
            }
        }
    });
    ivAttachment4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(strPhoto4 == "" && PageType.equals("NewRequestMedical")) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
                photoTemp = "photo4";
            }
            else if(strPhoto4 == "" && !PageType.equals("NewRequestMedical"))
            {
                Toast.makeText(getApplicationContext(), "there is no attachment on this request",
                        Toast.LENGTH_SHORT).show();
            }else
            {
                Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);
                Bundle extras = new Bundle();
                extras.putString("photo", strPhoto4);
                extras.putString("photoNo","photo4");
                extras.putString("PageTypeRequest",PageType);
                i.putExtras(extras);
                startActivityForResult(i,100);
               /* i.putExtra("photo", strPhoto4);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);*/
            }
        }
    });
    ivAttachment5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(strPhoto5 == "" && PageType.equals("NewRequestMedical")) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
                photoTemp = "photo5";
            }
            else if(strPhoto5 == "" && !PageType.equals("NewRequestMedical"))
            {
                Toast.makeText(getApplicationContext(), "there is no attachment on this request",
                        Toast.LENGTH_SHORT).show();
            }
            else{
                Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);
                Bundle extras = new Bundle();
                extras.putString("photo", strPhoto5);
                extras.putString("photoNo","photo5");
                extras.putString("PageTypeRequest",PageType);
                i.putExtras(extras);
                startActivityForResult(i,100);
               /* i.putExtra("photo", strPhoto5);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);*/
            }
        }
    });

现在我只需制作10张图像视图并声明全部。这不是动态的。我将照片转换为位图,然后将其发送到数据库。

我可以使用任何方式或库吗?

1 个答案:

答案 0 :(得分:0)

您可以拥有如下所示的“添加图片”按钮,而不是拥有多张图片。 enter image description here

然后在“添加图片”按钮上单击,您可以使用startactivityforresults()来获取图像,使用onAcivityResult()可以创建imageView并将其添加到容器并上传相同的图片。

    public void addImageView(Context context, View container){
       ImageView imgNew = new ImageView(context);
       //add layout params & set image
       container.addView(newImage);
       // start service to upload the image
    }
相关问题