将4张图像从活动传递到其他活动

时间:2018-06-11 16:14:10

标签: android android-layout

我的应用由8 Buttons和4 ImageViews组成。 4个按钮是"浏览"按钮和4个按钮是"取消"的按钮。

到目前为止我所看到的是点击浏览时我们从图库中选择了一张图片并将其显示在ImageViews中,当我们点击取消按钮时,它会清除ImageView

所以每2个按钮就是"浏览"按钮和"取消"按钮连接到一个ImageView。和...等其他按钮和ImageViews

所以我需要实现的是当用户点击" NEXT"时,我需要将4个图像传递给下一个活动。按钮,4 ImageViews转移到另一个活动,但下一个活动不包含ImageViews来显示我的传递图像。

所以我需要传递这些图片,因为我使用了extragetExtraImageViews.

所以我需要实现的是:我需要传递图像,但是如果某些ImageView没有图片。没有通过,但如果ImageViews包含一些图片。传递这些图像取决于用户"用户将添加多少张图片"但是不要在下一个活动中显示传递的图像。

因为我要把按钮叫做"上传"用户单击该上载的位置将图像作为base64上传到DB。

那么你如何传递图像并使其像注册系统一样隐藏在用户之外。??

Attachment_Class

public class Attachment_class extends AppCompatActivity  {

ImageView img_One;
Button btn_Browse_Image_One;
Button btn_Cancel_Image_One;

ImageView img_Two;
Button btn_Browse_Image_Two;
Button btn_Cancel_Image_Two;

ImageView img_Three;
Button btn_Browse_Image_Three;
Button btn_Cancel_Image_Three;

ImageView img_Four;
Button btn_Browse_Image_Four;
Button btn_Cancel_Image_Four;

Button Next;
Uri imageUri;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.attachment);

    img_One = (ImageView) findViewById(R.id.ImgView_One);
    btn_Browse_Image_One = (Button) findViewById(R.id.Broswe_image_one);
    btn_Cancel_Image_One = (Button) findViewById(R.id.Cancel_ImgView_One);

    img_Two = (ImageView) findViewById(R.id.ImgView_Two);
    btn_Browse_Image_Two = (Button) findViewById(R.id.Broswe_image_two);
    btn_Cancel_Image_Two = (Button) findViewById(R.id.Cancel_ImgView_Two);

    img_Three = (ImageView) findViewById(R.id.ImgView_Three);
    btn_Browse_Image_Three = (Button) findViewById(R.id.Broswe_image_Three);
    btn_Cancel_Image_Three = (Button) findViewById(R.id.Cancel_ImgView_Three);

    img_Four = (ImageView) findViewById(R.id.ImgView_Four);
    btn_Browse_Image_Four = (Button) findViewById(R.id.Broswe_image_Four);
    btn_Cancel_Image_Four = (Button) findViewById(R.id.Cancel_ImgView_Four);

    //btn_Browse_Image_One.setOnClickListener(this);
    // btn_Cancel_Image_One.setOnClickListener(this);
    // Next.setOnClickListener(this);

    btn_Browse_Image_One.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, 101);
        }
    });

    btn_Cancel_Image_One.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            img_One.setImageDrawable(null);
        }
    });

    btn_Browse_Image_Two.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent x = new Intent(Intent.ACTION_GET_CONTENT);
            x.setType("image/*");
            startActivityForResult(x, 102);
        }
    });

    btn_Cancel_Image_Two.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            img_Two.setImageDrawable(null);
        }
    });

    btn_Browse_Image_Three.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent x = new Intent(Intent.ACTION_GET_CONTENT);
            x.setType("image/*");
            startActivityForResult(x, 103);

        }
    });

    btn_Cancel_Image_Three.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            img_Three.setImageDrawable(null);
        }
    });

    btn_Browse_Image_Four.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent x = new Intent(Intent.ACTION_GET_CONTENT);
            x.setType("image/*");
            startActivityForResult(x, 104);

        }
    });

    btn_Cancel_Image_Four.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            img_Four.setImageDrawable(null);
        }
    });

    /*
    Next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent1 = new Intent(Attachment_class.this, Upload_Class.class);
       //     Bitmap bitmap = img_One;  // take from img view
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, bs); // it leads to error
            intent1.putExtra("byteArray", _bs.toByteArray());
            startActivity(intent);

        }
    });
    */

    Next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(imageUri != null)
            {
                Intent x = new Intent(Attachment_class.this , Upload_Class.class);

            }
        }
    });


}

protected void onActivityResult(int requestCode, int resultCode, final Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 101 && resultCode == RESULT_OK)
    {
        Uri uri = data.getData();
        imageUri = uri;
        Picasso.get().load(uri).fit().centerCrop().into(img_One);
        // Add the recent dependency for Picasso
    }

    if(requestCode == 102 && resultCode == RESULT_OK)
    {
        Uri uri = data.getData();
        imageUri = uri;
        Picasso.get().load(uri).fit().centerCrop().into(img_Two);
    }

    if(requestCode == 103 && resultCode == RESULT_OK)
    {
        Uri uri = data.getData();
        imageUri = uri;
        Picasso.get().load(uri).fit().centerCrop().into(img_Three);
    }

    if(requestCode == 104 && resultCode == RESULT_OK)
    {
        Uri uri = data.getData();
        imageUri = uri;
        Picasso.get().load(uri).fit().centerCrop().into(img_Four);
    }
}
}

Upload.class

public class Upload_Class extends Attachment_class {


Button bt;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.the_openion);
    //btn = (Button) findViewById(R.id.Upload_Btn);
}}

1 个答案:

答案 0 :(得分:0)

尝试将您的图像放在一个实现Parcelable的类中,因此在您的意图中使用intent.putExtra(" yourClass",yourClass);

并且在接收您使用的意图的活动中

Intent it = getIntent(); YourClass yourClass = it.getParcelableExtra(" yourClass");