Home > Back-end >  How to pass image from one activity to another activity
How to pass image from one activity to another activity

Time:12-05

I am getting picture from gallery using this code.i want to pass this image view to another activity and show it.In second activity i want to catch it.It is same as data passing through an activities but the images are not working in that method.Any suggetions?

    val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
        
        imageView.setImageURI(uri)
    }
    val button = findViewById<Button>(R.id.button)
    button.setOnClickListener {
        
        getContent.launch("image/*")

    }

    val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
        
        imageView.setImageURI(uri)
    }

    val button = findViewById<Button>(R.id.button)

    button.setOnClickListener {
        
        getContent.launch("image/*")

    }

CodePudding user response:

Convert the URI to string and add it to intent in your First activity

intent.putExtra("img_uri", selectedImage.toString());

In your second activity get the string and parse the string to get uri

Intent intent = getIntent(); 
String uriStr = intent.getStringExtra("img_uri"); 
Uri uri = Uri.parse(uriStr);
imageview.setImageURI(uri) ;

To Start Second Activity

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("img_uri", selectedImage.toString());
startActivity(intent);

CodePudding user response:

Instead of .getContent() use .getDocument() and take persistable uri permission in on activity result.

The rest the same as proposed by Sidharth Mudgil.

  • Related