首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >registerForActivityResult resultCode得到0值

registerForActivityResult resultCode得到0值
EN

Stack Overflow用户
提问于 2022-06-28 16:13:39
回答 2查看 202关注 0票数 1

我尝试使用registerForActivityResult,但是结果是0,这意味着它不能从活动中得到结果。上个月我这么做的时候,代码运行得很好,但是我不知道为什么它今天会出错,我试着检查代码中的错误,但是我认为没有错误。

下面是使用照相机的功能:

代码语言:javascript
复制
private fun UseCamera() {
        val takePictureIntent = Intent (MediaStore.ACTION_IMAGE_CAPTURE)
        val imagePath = File(Environment.getExternalStorageDirectory(), "Pictures")
        val photoFile = File(imagePath, "my_picture.jpg")
        FilePath = FileProvider.getUriForFile(this, FILE_AUTHORITY, photoFile )
        Log.w("FilePath",FilePath.toString())
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FilePath)
        takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
        getPreviewImage.launch(takePictureIntent)
        Log.w("UseCamera","Successful")
    }

下面是registerForResultActivity:

代码语言:javascript
复制
        getPreviewImage = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
                result -> Log.e("Preview Image", result.resultCode.toString())
            if (result.resultCode == RESULT_OK) {
                if (!forfood){
                Log.i("File Path", FilePath.toString())
                val SelectedImage = FilePath
                val PicRef = StorageRef.child(preferences.getValue("username").toString())
                    .child("kiosk_pic/" + ImageID)
                PicRef.putFile(SelectedImage).addOnSuccessListener {
                    Toast.makeText(this, "Uploaded", Toast.LENGTH_SHORT).show()
                    PicRef.downloadUrl.addOnSuccessListener {
                        preferences.setValue("kiosk_pic", it.toString())
                        ref.child(preferences.getValue("username").toString()).child("kiosk_picture").setValue(it.toString())
                        setKioskImage(preferences.getValue("kiosk_pic").toString(),ImageID)
                    }
                }
                    .addOnFailureListener {
                        Toast.makeText(this, "Upload Fail", Toast.LENGTH_SHORT).show()
                    }
                    .addOnProgressListener {
                        Toast.makeText(this, "Uploading", Toast.LENGTH_SHORT).show()
                    }
            } else {
                    Log.i("File Path",FilePath.toString())
                    val SelectedImage = FilePath
                    if (add){
                        iv_addimage.setImageURI(SelectedImage)
                    }else{
                        iv_changeimage.setImageURI(SelectedImage)
                    }
                }
        }
        }

我添加了日志,结果是每次我使用相机时:

代码语言:javascript
复制
W/UseCamera: Successfull
D/OpenGLRenderer: endAllActiveAnimators on 0xea49f7f0 (AlertController$RecycleListView) with handle 0xc1acc9b0
E/Preview Image: 0

我在这里做错了什么?因为它以前运作得很好

编辑,日志中也这样写着:

代码语言:javascript
复制
W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-06-30 08:36:25

我已经通过更改UseCamera函数代码来解决这个问题。我不太确定,但我认为问题在于:

代码语言:javascript
复制
        val imagePath = File(Environment.getExternalStorageDirectory(), "Pictures")
        val photoFile = File(imagePath, "my_picture.jpg")

因为我不太确定,我猜是在照片拍摄后创建的临时文件丢失了,所以结果代码返回RESULT_CANCELED。

我将代码改为:

代码语言:javascript
复制
    private fun UseCamera() {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        val imageFile = getExternalFilesDir (Environment.DIRECTORY_PICTURES)
        val tempFile = File.createTempFile("my_picture",".jpg",imageFile)
        FilePath = FileProvider.getUriForFile(this, FILE_AUTHORITY,tempFile)
        Log.e("File Path", FilePath.toString())
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,FilePath)
        getPreviewImage.launch(takePictureIntent)
}

并将提供程序路径更改为

代码语言:javascript
复制
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="picture" path="Android/data/com.example.njajal/files/Pictures"/>
</paths>

我希望我的回答,帮助一些有同样问题的人

票数 0
EN

Stack Overflow用户

发布于 2022-08-26 07:29:10

如果任何使用java的用户都使用它,那么首先将其定义为oncreate方法:

代码语言:javascript
复制
 ActivityResultLauncher<Intent> mStartForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
       Log.d("Capture_img", ": "+result.getData());
       if (result.getResultCode()==Activity.RESULT_OK){
           Intent intent = result.getData();
           Uri uri = Objects.requireNonNull(intent).getData();
           String path = uri.getPath();
           File ff = new File("" +path);
           String  pffImage = ff.getName();
           String path111 =  GloabalMethods.imageCompressor(getContext(), path,pffImage);
           File user_img = new File(""+path111);
           Bitmap myBitmap = BitmapFactory.decodeFile(user_img.getAbsolutePath());
           ByteArrayOutputStream bytes = new ByteArrayOutputStream();
           myBitmap.compress(Bitmap.CompressFormat.JPEG,80,bytes);
       binaryImage = Base64.encodeToString(bytes.toByteArray(),Base64.DEFAULT);
           Log.d("RRRRRRRRRRRRRRRR", "onActivityResult: "+uri);
           capture_image.setImageBitmap(myBitmap);
           capture_image.setTag("yes");
       } after that call 
 ImagePicker.with(requireActivity()) .crop() //Crop image(Optional), Check Customization for more option .compress(1024)
//Final image size will be less thanMB(Optional) 
.maxResultSize(1080, 1080).createIntent(intent -> { mStartForResult.launch(intent); return null; } );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72789925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档