简体   繁体   中英

How to solve Photopicker error in android Studio?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM