android工作室如何将多张选中的照片分别保存上传到服务器?

How to save multiple selected photos separately to upload to a server in android studio?

我已经遇到了几个星期的问题,但仍然无法解决,做了很多研究并测试了很多代码,但没有解决。我会详细解释我的问题。

我正在制作一个应用程序,在布局中会有三张或更多可点击的照片,我正在使用“ArthurHub / Android-Image-Cropper”图像裁剪库,步骤是:

  1. Click on the image, which gives the option to open the image gallery to select a photo or take a new photo, after that I can crop the selected image.
    1. Select the second photo, do the same as the previous one, thereafter.
    2. Upload the photos to the server.

步骤 1 工作正常,当我尝试从第二张图片 select 时出现问题。 我正在使用什么:我正在使用两个 classes:一个请求图像,另一个 returns: 1. 将是 ImageButton 的主要 window。 2. 另一个 class returns 将 select 图像编辑到第一个 class。 (包含调用图库或相机的函数,剪切图像,另一个returns图像地址的“onActivityResult”函数,谁要的图片)。 到目前为止一切正常。

问题描述:问题出在第二张图片selected上。当第二个 class returns 一切都到第一个时,就好像第二个图像存储在与第一个相同的内存 space 中(删除之前的所有内容),如果我 select 第三个图像,它删除第二个,只保留第三个,以后。我想要做的是 select 图像并让它们同时显示(可用)以发送到服务器。

我尝试过的解决方案:

  1. After days of searching, the suggestion was to make several returns on “onActivityResult” so that it returned the result of selecting images separately, I couldn't find anything that worked, the explanations I found were only halfway (including the official documentation does not detail the steps to control the various returns of the function, is very superficial), could not control the separate pointing of the images.
switch (requestCode){
                case (1000):
                    Intent intent1 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
                    intent1.putExtra("class", classname);
                    intent1.putExtra("imageview", imageview);
                    intent1.putExtra("pathimage", pathimage);
                    startActivity(intent1);

                    break;
                case (2000):
                    Intent intent2 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
                    intent2.putExtra("class", classname);
                    intent2.putExtra("imageview", imageview);
                    intent2.putExtra("pathimage", pathimage);
                    startActivity(intent2);
                case (3000):
                    Intent intent3 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
                    intent3.putExtra("class", classname);
                    intent3.putExtra("imageview", imageview);
                    intent3.putExtra("pathimage", pathimage);
                    startActivity(intent3);
                    break;
                default: break;
            }
  1. I decided to save each image separately outside the “temporary cache” folder (where they are located), even with the “manifest” permissions, nothing happens, permission is denied, so I couldn't even create the new folder to save the images in it, as a result does not save the selected images.
 if (ContextCompat.checkSelfPermission(TirarFoto1.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){

                File file = new File(Environment.getExternalStoragePublicDirectory(String.valueOf(imageUri))+"folderName");
                if (!file.exists()){
                    Toast.makeText(this, "exists", Toast.LENGTH_SHORT).show();
                }
                if (success){
                    Toast.makeText(this, "creaty", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, "Erro!!!", Toast.LENGTH_SHORT).show();
                }
            }else {
                requestStoragePermisson();
            }

结果:第一个解决方案无效,第二个解决方案无效。 我想要什么:一个模型,一种帮助我的具体方法,有人可以帮助我吗?被这个问题困了好久

我使用ArthurHub-Android-Image-Cropper来裁剪图片,你可以借鉴一下link

https://github.com/ArthurHub/Android-Image-Cropper/wiki

希望对您有所帮助

编辑: 我有一个 class 使用 ArthurHub-Android-Image-Cropper 但来自 kotlin,我尝试转换为 java 像这样

//for identify image
private Int imageNo;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //......

    //set button image 1
    final Button button1 = findViewById(R.id.button_image1);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // set image count to 1
            imageNo=1
            getImageClick()
        }
    });

    //set button image 2
    final Button button1 = findViewById(R.id.button_image1);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // set image count to 2
            imageNo=2
            getImageClick()
        }
    });

    //set button image 3
    final Button button1 = findViewById(R.id.button_image1);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // set image count to 3
            imageNo=3
            getImageClick()
        }
    });
}

//select image form camera or galery 
public void getImageClick() {
    CropImage.startPickImageActivity(this);
}

//this for Crope Image
private void startCropImageActivity(Uri imageUri) {
  CropImage.activity(imageUri)
    .start(this);
}

@Override
@SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    //handle result from onGetimageClick(button for select image from camera or galery)
    if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == AppCompatActivity.RESULT_OK) {
        Uri imageUri = CropImage.getPickImageResultUri(this, data);
        //start Crope image
        startCropImageActivity(imageUri);
    }

    // handle result of CropImageActivity
    else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        Uri resultUri = result.getUri();
        switch (imageNo){
            case (1):
                //here you have resultUri for save image or preview as image1
                break;
            case (2):
                //here you have resultUri for save image or preview as image1
            case (3):
                //here you have resultUri for save image or preview as image1
        }
    }
}

别忘了添加清单

<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
        android:theme="@style/Theme.AppCompat"/>

希望对您有所帮助