0

I have the following code , im receiving an error :

enter image description here

package com.example.photopicker;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button addimage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addimage=findViewById(R.id.button_pick_photo);
       addimage.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
            // Registers a photo picker activity launcher in single-select mode.
               ActivityResultLauncher<PickVisualMediaRequest> pickMedia =
                       registerForActivityResult(new ActivityResultContracts.PickVisualMedia(), uri -> {
                           // Callback is invoked after the user selects a media item or closes the
                           // photo picker.
                           if (uri != null) {
                               Log.d("PhotoPicker", "Selected URI: " + uri);
                           } else {
                               Log.d("PhotoPicker", "No media selected");
                           }
                       });

            // Include only one of the following calls to launch(), depending on the types
            // of media that you want to allow the user to choose from.

             // Launch the photo picker and allow the user to choose images and videos.
               pickMedia.launch(new PickVisualMediaRequest.Builder()
                       **.setMediaType(new  ActivityResultContracts.PickVisualMedia.ImageAndVideo())**
                       .build());
           }
       });
    }
}

This code i got it from the Android developer Website : https://developer.android.com/training/data-storage/shared/photopicker

but Doesnt seem to work , and im not able to find any online solution.

2 Answers2

0

Try replacing:

new ActivityResultContracts.PickVisualMedia.ImageAndVideo()

with:

ActivityResultContracts.PickVisualMedia.Companion.getImageAndVideo()

ImageAndVideo is a Kotlin object — it is not a class that you instantiate yourself. However, the source code lacks the @JvmField annotation, so I think that just referring to ActivityResultContracts.PickVisualMedia.ImageAndVideo will fail, as outlined in the docs.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Error : Required type: VisualMediaType Provided: ImageAndVideo – Saleem Manasfi Jan 16 '23 at 18:21
  • @SaleemManasfi: Sorry, try the updated answer. I do not reference Kotlin code from Java much, so I am rusty on the "interop" process. I recommend that you learn Kotlin and switch to that, as Android is steadily moving away from Java for mainstream programming. – CommonsWare Jan 16 '23 at 18:28
  • Thank you , the Kotelin Code worked , yet the Java Code is not working , weird how google never noticed this !! is there a way to make it work in java that im missing ? – Saleem Manasfi Jan 17 '23 at 08:30
0

I just worked this out yesterday. It is very simple code of few lines yet took me entire day to figure out what was wrong.Just add the following snippet of your code outside the clicklistener on the top. Seems like the variable doesn't initialize in time for it to launch hence the error.

ActivityResultLauncher<PickVisualMediaRequest> pickMedia =
                       registerForActivityResult(new ActivityResultContracts.PickVisualMedia(), uri -> {
                           // Callback is invoked after the user selects a media item or closes the
                           // photo picker.
                           if (uri != null) {
                               Log.d("PhotoPicker", "Selected URI: " + uri);
                           } else {
                               Log.d("PhotoPicker", "No media selected");
                           }
                       });

-------------------------------------------------------------
************** MY CODE FOR REFERENCE*************************
-------------------------------------------------------------
class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityMainBinding
    private val REQUEST_CODE_SINGLE_SELECT = 1
    private var pickMedia = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
        // Callback is invoked after the user selects a media item or closes the
        // photo picker.
        if (uri != null) {
            Log.d("PhotoPicker", "Selected URI: $uri")
        } else {
            Log.d("PhotoPicker", "No media selected")
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setSupportActionBar(binding.toolbar)

        val navController = findNavController(R.id.nav_host_fragment_content_main)
        appBarConfiguration = AppBarConfiguration(navController.graph)
        setupActionBarWithNavController(navController, appBarConfiguration)

        binding.fab.setOnClickListener {
            // Launch the photo picker and let the user choose only images.
            pickMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
        }
    }
starball
  • 20,030
  • 7
  • 43
  • 238
Uroos Fatima
  • 1
  • 1
  • 1