3

I believe many people have asked this question long time ago. Now, startActivityForResult is depreciated and I am looking for its replacement.

Previous code would be

        public void onClick(View v) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(
                Intent.createChooser(
                        intent,
                        "Select Image from here..."),
                PICK_CODE);
die
  • 57
  • 1
  • 8
  • See https://developer.android.com/reference/androidx/activity/result/contract/ActivityResultContracts.GetContent and https://stackoverflow.com/a/63339127/115145 – CommonsWare Apr 01 '21 at 17:05
  • Did you already read the [documentation on getting a result from an activity](https://developer.android.com/training/basics/intents/result)? It uses `GetContent` as its example. – ianhanniballake Apr 01 '21 at 17:54
  • Yes, I read those documents. While I am using startActivityForResult (since it's simpler), I will try to move toward using registerActivity. Thanks for all suggested documents. – die Apr 02 '21 at 03:24

2 Answers2

2

Use this,

ImageView image;

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

    image = (ImageView) findViewById(R.id.display_image);
}

public void pickAImage(View view) 
{
 Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
 photoPickerIntent.setType("image/*");
 someActivityResultLauncher.launch(photoPickerIntent);
}

Then do the operation,

ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        result -> {
            if (result.getResultCode() == Activity.RESULT_OK) {
                // There are no request codes
                // doSomeOperations();
                Intent data = result.getData();
                Uri selectedImage = Objects.requireNonNull(data).getData();
                InputStream imageStream = null;
                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                BitmapFactory.decodeStream(imageStream);
                image.setImageURI(selectedImage);// To display selected image in image view
            }
        });
HAZEEM JOONUS
  • 483
  • 6
  • 9
  • 1
    Very thanks for this your answer. I needed new way code without startActivityForResult() for directly open gallery via Intent.ACTION_PICK. – moorker Jan 26 '22 at 23:10
0

While startActivityForResult is still working, it has been marked as "depreciated" in my android studio. Documents provided by CommonsWare and ianhanniballake are helpful. I also found that someone has asked this question before and there is an answer for it.

OnActivityResult method is deprecated, what is the alternative?.

die
  • 57
  • 1
  • 8