onClickListener on imageview to start new intent

前端 未结 2 448
灰色年华
灰色年华 2021-01-17 01:11

So Im trying to use an onClickListener to pass an image from one activity to a new activity but I cannot figure out how to do it. How it works is the app starts with a listv

2条回答
  •  梦毁少年i
    2021-01-17 01:45

    Use your image as ByteArray. This worked for me without any problem. In your click listener of the sender activity do the following,

    Intent senderint = new Intent(context, receiveractivity.class);
    Bitmap bitmap = drawableToBitmap(view.getBackground());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] b = baos.toByteArray();
    senderint.putExtra("myimage", b);
    startActivity(senderint);
    

    And in your receiver activity do the following.

    private void getImage(){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = null;
    v = inflater.inflate(R.layout.receiverlayout, null);
    
    ViewGroup vg = (ViewGroup) findViewById(R.id.yourmainlayout);
    Bundle bundle = getIntent().getExtras();
    byte[] ba = bundle.getByteArray("myimage");
    Bitmap bm = BitmapFactory.decodeByteArray(ba, 0, ba.length());
    Drawable dr = new BitmapDrawable(getResources, bm);
    vg.setBackground(dr);
    vg.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }
    

    call getImage() in your oncreate method of your receiver activity. The context is your application context. Good luck:)

提交回复
热议问题