Created
November 16, 2022 09:52
-
-
Save mebjas/c44808589bb95ec288945b097dc2b687 to your computer and use it in GitHub Desktop.
Example of Android activity with image picker.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
private final ActivityResultLauncher<String[]> galleryActivityLauncher | |
= registerForActivityResult(new ActivityResultContracts.OpenDocument(), | |
this::onPickImage); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
// Assuming this is called on clicking a button or something. | |
public void pickImage(View unused) { | |
galleryActivityLauncher.launch(new String[]{"image/*"}); | |
} | |
private void onPickImage(Uri imageUri) { | |
// TODO: Load the selected image from URI. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment