我有以下代码,我收到一个错误:
输入图片描述
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
但似乎不工作,我无法找到任何在线解决方案。
尝试替换:
new ActivityResultContracts.PickVisualMedia.ImageAndVideo()
:
ActivityResultContracts.PickVisualMedia.Companion.getImageAndVideo()
ImageAndVideo
是Kotlinobject
-它不是一个您自己实例化的类。然而,源代码缺少@JvmField
注释,所以我认为仅仅引用ActivityResultContracts.PickVisualMedia.ImageAndVideo
将失败,正如文档中概述的那样。
我昨天才算出来。这是一个非常简单的代码,只有几行,但我花了一整天的时间才弄清楚哪里出了问题。只需在顶部的clicklistener之外添加以下代码片段。似乎变量没有及时初始化以使其启动,因此出现错误。
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))
}
}
使用
new PickVisualMediaRequest.Builder()
.setMediaType(PickVisualMedia.ImageAndVideo.INSTANCE)
.build()
它可能会显示一个错误,但它仍然可以正常编译。