Skip to content

Instantly share code, notes, and snippets.

@mebjas
Created November 16, 2022 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mebjas/c44808589bb95ec288945b097dc2b687 to your computer and use it in GitHub Desktop.
Save mebjas/c44808589bb95ec288945b097dc2b687 to your computer and use it in GitHub Desktop.
Example of Android activity with image picker.
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