How to solve Photopicker error in android Studio?

1k Views Asked by At

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.

3

There are 3 best solutions below

3
On

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.

0
On

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))
        }
    }
0
On

Use this:

new PickVisualMediaRequest.Builder()
    .setMediaType(PickVisualMedia.ImageAndVideo.INSTANCE)
    .build()

It may show an error, but it will still compile fine.