如何通过相机或图库动态添加图像?

Posted

技术标签:

【中文标题】如何通过相机或图库动态添加图像?【英文标题】:How can I Add images dynamically through Camera or Gallery? 【发布时间】:2017-08-25 05:49:08 【问题描述】:

我正在制作一个 android 应用程序,允许用户通过相机单击图片或通过图库添加图像。

图像视图中只允许添加 5 张图像。我如何在运行时在活动中动态添加图像视图。

如何删除和重新定位活动中存在的图像。

因为有 5 张图片,所以当我删除第 2 张图片时,第 3、第 4 和第 5 张图片应该重新定位到第 2、第 3 和第 4 张图片,它应该允许我添加第 5 张图片。

任何人都可以帮助我提供源代码。

这是我的代码:

private static final int CAMERA_REQUEST_FIRST_IMAGE = 100;
    private static final int CAMERA_REQUEST_SECOND_IMAGE = 101;
    private static final int CAMERA_REQUEST_THIRD_IMAGE = 102;
    private static final int CAMERA_REQUEST_FOURTH_IMAGE = 103;
    private static final int CAMERA_REQUEST_FIFTH_IMAGE = 104;
    private static final int PICK_PICTURE_FIRST_IMAGE = 105;
    private static final int PICK_PICTURE_SECOND_IMAGE = 106;
    private static final int PICK_PICTURE_THIRD_IMAGE = 107;
    private static final int PICK_PICTURE_FOURTH_IMAGE = 108;
    private static final int PICK_PICTURE_FIFTH_IMAGE = 109;
    Boolean imageclick_1 = false, imageclick_2 = false, imageclick_3 = false, imageclick_4 = false, imageclick_5 = false;
    TextView textview_imagename1, textview_imagename2, textview_imagename3, textview_imagename4, textview_imagename5;
ImageView imageView1, imageView2, imageView3, imageView4, imageView5;
imageView1 = (ImageView) findViewById(R.id.imageview1);
        imageView1.setOnClickListener(this);
 imageView2 = (ImageView) findViewById(R.id.imageview2);
        imageView2.setOnClickListener(this);
 imageView3 = (ImageView) findViewById(R.id.imageview3);
        imageView3.setOnClickListener(this);
 imageView4 = (ImageView) findViewById(R.id.imageview4);
        imageView4.setOnClickListener(this);
 imageView5 = (ImageView) findViewById(R.id.imageview5);
        imageView5.setOnClickListener(this);

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) 
            case PICK_PICTURE_FIRST_IMAGE:
                if (resultCode == Activity.RESULT_OK) 
                    Uri selectedImage1 = data.getData();
                    String s1 = getRealPathFromURI(selectedImage1);
                    try 
                        images1 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage1);
                     catch (IOException e) 
                        e.printStackTrace();
                    
                    imageView1.setImageURI(selectedImage1);
                    String name1 = s1.substring(s1.lastIndexOf("/") + 1);
                    textview_imagename1.setText(name1);
                    imageView2.setVisibility(View.VISIBLE);
                    imageclick_1 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                
                break;
            case PICK_PICTURE_SECOND_IMAGE:
                if (resultCode == Activity.RESULT_OK) 
                    Uri selectedImage2 = data.getData();
                    String s2 = getRealPathFromURI(selectedImage2);
                    try 
                        images2 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage2);
                     catch (IOException e) 
                        e.printStackTrace();
                    
                    imageView2.setImageURI(selectedImage2);
                    String name2 = s2.substring(s2.lastIndexOf("/") + 1);
                    textview_imagename2.setText(name2);
                    imageView3.setVisibility(View.VISIBLE);
                    imageclick_2 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                
                break;
            case PICK_PICTURE_THIRD_IMAGE:
                if (resultCode == Activity.RESULT_OK) 
                    Uri selectedImage3 = data.getData();
                    String s3 = getRealPathFromURI(selectedImage3);
                    try 
                        images3 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage3);
                     catch (IOException e) 
                        e.printStackTrace();
                    
                    imageView3.setImageURI(selectedImage3);
                    String name3 = s3.substring(s3.lastIndexOf("/") + 1);
                    textview_imagename3.setText(name3);
                    imageView4.setVisibility(View.VISIBLE);
                    imageclick_3 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                
                break;
            case PICK_PICTURE_FOURTH_IMAGE:
                if (resultCode == Activity.RESULT_OK) 
                    Uri selectedImage4 = data.getData();
                    String s4 = getRealPathFromURI(selectedImage4);
                    try 
                        images4 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage4);
                     catch (IOException e) 
                        e.printStackTrace();
                    
                    imageView4.setImageURI(selectedImage4);
                    String name4 = s4.substring(s4.lastIndexOf("/") + 1);
                    textview_imagename4.setText(name4);
                    imageView5.setVisibility(View.VISIBLE);
                    imageclick_4 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                
                break;
            case PICK_PICTURE_FIFTH_IMAGE:
                if (resultCode == Activity.RESULT_OK) 
                    Uri selectedImage5 = data.getData();
                    String s5 = getRealPathFromURI(selectedImage5);
                    try 
                        images5 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage5);
                     catch (IOException e) 
                        e.printStackTrace();
                    
                    imageView5.setImageURI(selectedImage5);
                    String name5 = s5.substring(s5.lastIndexOf("/") + 1);
                    textview_imagename5.setText(name5);
                    imageclick_5 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                
                break;
            case CAMERA_REQUEST_FIRST_IMAGE:
                if (resultCode == Activity.RESULT_OK) 
                    photo1 = (Bitmap) data.getExtras().get("data");
                    imageView1.setImageBitmap(photo1);
                    SaveImage(photo1);
                    textview_imagename1.setText(fname);
                    imageView2.setVisibility(View.VISIBLE);
                    imageclick_1 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                
                break;
            case CAMERA_REQUEST_SECOND_IMAGE:
                if (resultCode == Activity.RESULT_OK) 
                    photo2 = (Bitmap) data.getExtras().get("data");
                    imageView2.setImageBitmap(photo2);
                    SaveImage(photo2);
                    textview_imagename2.setText(fname);
                    imageView3.setVisibility(View.VISIBLE);
                    imageclick_2 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                
                break;
            case CAMERA_REQUEST_THIRD_IMAGE:
                if (resultCode == Activity.RESULT_OK) 
                    photo3 = (Bitmap) data.getExtras().get("data");
                    imageView3.setImageBitmap(photo3);
                    SaveImage(photo3);
                    textview_imagename3.setText(fname);
                    imageView4.setVisibility(View.VISIBLE);
                    imageclick_3 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                
                break;
            case CAMERA_REQUEST_FOURTH_IMAGE:
                if (resultCode == Activity.RESULT_OK) 
                    photo4 = (Bitmap) data.getExtras().get("data");
                    imageView4.setImageBitmap(photo4);
                    SaveImage(photo4);
                    textview_imagename4.setText(fname);
                    imageView5.setVisibility(View.VISIBLE);
                    imageclick_4 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                
                break;
            case CAMERA_REQUEST_FIFTH_IMAGE:
                if (resultCode == Activity.RESULT_OK) 
                    photo5 = (Bitmap) data.getExtras().get("data");
                    imageView5.setImageBitmap(photo5);
                    SaveImage(photo5);
                    textview_imagename5.setText(fname);
                    imageclick_5 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                
                break;
        
    

private void SaveImage(Bitmap finalBitmap) 
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/saved_images");
        myDir.mkdirs();
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        fname = "Image-" + timeStamp + ".jpg";
        File file = new File(myDir, fname);
        if (file.exists()) file.delete();
        try 
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
         catch (Exception e) 
            e.printStackTrace();
        
    

@Override
    public void onClick(View v) 
        switch (v.getId()) 
            case R.id.imageview1:
                if (imageclick_1) 
                    fullimageview.setVisibility(View.VISIBLE);
                    Bitmap bitmap = ((BitmapDrawable) imageView1.getDrawable()).getBitmap();
                    fullimageview.setImageBitmap(bitmap);
                 else 
                    final String[] Items = "Click Pic", "Pick Pic", "Cancel";
                    final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Add Photo");
                    builder.setItems(Items, new DialogInterface.OnClickListener() 
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) 
                            if (i == 0) 
                                isdialogset = "CAMERA";
                                if (imageView1.getDrawable() == null) 
                                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                    startActivityForResult(takePicture, CAMERA_REQUEST_FIRST_IMAGE);
                                
                             else if (i == 1) 
                                isdialogset = "GALLERY";
                                Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//                                pickPhoto.setDataAndType(Uri.fromFile(new File(path)))
                                startActivityForResult(pickPhoto, PICK_PICTURE_FIRST_IMAGE);
                             else 
                                dialogInterface.dismiss();
                            
                        
                    );
                    builder.show();
                
                break;
            case R.id.imageview2:
                if (imageclick_2) 
                    fullimageview.setVisibility(View.VISIBLE);
                    Bitmap bitmap = ((BitmapDrawable) imageView2.getDrawable()).getBitmap();
                    fullimageview.setImageBitmap(bitmap);
                 else if (isdialogset.equals("CAMERA") && imageView2.getDrawable() == null) 
                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(takePicture, CAMERA_REQUEST_SECOND_IMAGE);
                 else if (isdialogset.equals("GALLERY")) 
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(pickPhoto, PICK_PICTURE_SECOND_IMAGE);
                
                break;
            case R.id.imageview3:
                if (imageclick_3) 
                    fullimageview.setVisibility(View.VISIBLE);
                    Bitmap bitmap = ((BitmapDrawable) imageView3.getDrawable()).getBitmap();
                    fullimageview.setImageBitmap(bitmap);
                 else if (isdialogset.equals("CAMERA") && imageView3.getDrawable() == null) 
                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(takePicture, CAMERA_REQUEST_THIRD_IMAGE);
                 else if (isdialogset.equals("GALLERY")) 
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(pickPhoto, PICK_PICTURE_THIRD_IMAGE);
                
                break;
            case R.id.imageview4:
                if (imageclick_4) 
                    fullimageview.setVisibility(View.VISIBLE);
                    Bitmap bitmap = ((BitmapDrawable) imageView4.getDrawable()).getBitmap();
                    fullimageview.setImageBitmap(bitmap);
                 else if (isdialogset.equals("CAMERA") && imageView4.getDrawable() == null) 
                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(takePicture, CAMERA_REQUEST_FOURTH_IMAGE);
                 else if (isdialogset.equals("GALLERY")) 
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(pickPhoto, PICK_PICTURE_FOURTH_IMAGE);
                
                break;
            case R.id.imageview5:
                if (imageclick_5) 
                    fullimageview.setVisibility(View.VISIBLE);
                    Bitmap bitmap = ((BitmapDrawable) imageView5.getDrawable()).getBitmap();
                    fullimageview.setImageBitmap(bitmap);
                 else if (isdialogset.equals("CAMERA") && imageView5.getDrawable() == null) 
                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(takePicture, CAMERA_REQUEST_FIFTH_IMAGE);
                 else if (isdialogset.equals("GALLERY")) 
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(pickPhoto, PICK_PICTURE_FIFTH_IMAGE);
                
                break;
        
    

【问题讨论】:

请输入代码。代码 请检查编辑...抱歉耽搁了 请而不是点击向下箭头...尝试理解我的问题并帮助如果你能...如果你不能不向下箭头我的问题 如果我是对的,你想动态创建图像视图。 是 .... 我想动态创建 Imageview ... 应该只有 5 个 imageview ,,.. 并且想要添加删除功能 ... 如果删除了位置应该更改.. .. 【参考方案1】:

您可以像这样动态创建 ImageView:

 container = (LinearLayout)findViewById(R.id.container);
        for (int j = 0; j < 4; j++) 
            Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(takePicture, CAMERA_REQUEST);
            ImageView newimageview = new ImageView(MainActivity.this);
            newimageview.setBackgroundResource(R.drawable.panama_app_logo);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50,50);
            newimageview.setLayoutParams(params);
            container.addView(newimageview);
        

【讨论】:

谢谢@DK Thakur ...我会尝试使用这种方法解决 您可以使用 view.removeViewAt(index); 删除 imageview; RelativeLayout dynamicRelative = (RelativeLayout)findViewById(R.id.dynamicRelative);``int numberofimages = 4; for (int j = 0; j &lt; numberofimages; j++) Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(takePicture, CAMERA_REQUEST); newimageview = new ImageView(MainActivity.this); dynamicRelative.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));newimageview.setMaxHeight(50);newimageview.setMaxWidth(50); `dynamicRelative.addView(newimageview); @DK Thakur 你能检查一下这段代码有什么问题吗……它只显示一张图片 它正在添加所有但只显示一个,因为您使用的是RelativeLayout。如果您使用LinearLayout,您可以看到添加到视图中的所有图像。

以上是关于如何通过相机或图库动态添加图像?的主要内容,如果未能解决你的问题,请参考以下文章

如何将图像从相机/UIImagePickerController 动态添加到 iPhone 中的 iCarousel

Android - 如何从相机捕获图像或从库中添加

从图库上传图像或单击移动相机流星 android 应用程序中的图像

如何从图库和相机中裁剪图像

如何在 Android 7.0 中从相机或图库中选择要裁剪的图像?

如何在相机应用程序中添加裁剪功能