如何解决Photopicker错误在android工作室?

ego6inou  于 2023-01-19  发布在  Android
关注(0)|答案(1)|浏览(181)

我有以下代码,我收到一个错误:
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());
           }
       });
    }
}

这段代码我得到了它从Android开发者网站:https://developer.android.com/training/data-storage/shared/photopicker
但是似乎不起作用,而且我找不到任何在线解决方案。

xqnpmsa8

xqnpmsa81#

尝试替换:

new ActivityResultContracts.PickVisualMedia.ImageAndVideo()

与:

ActivityResultContracts.PickVisualMedia.Companion.getImageAndVideo()

ImageAndVideo是一个Kotlin object-它不是一个你自己示例化的类,但是源代码缺少@JvmField注解,所以我认为仅仅引用ActivityResultContracts.PickVisualMedia.ImageAndVideo是失败的,正如文档中所概述的那样。

相关问题