图像捕获后,Android Image FIle 变量变为空

问题描述

我目前正在开发的应用程序具有能够捕获图像、录制视频、浏览和上传图库中的媒体文件功能。问题是,该应用程序在模拟器中运行良好。没有任何错误(使用 android 9+ 模拟器检查)。但是该应用程序在物理设备(Android 10)上进行测试时表现出意外。出乎意料,因为视频录制和播放正常,以及从图库浏览和上传媒体。失败的是图像捕获。问题是实际上正在创建图像文件,但我在 photoFile 方法中将图像文件变量 null 值设为 onActivityResult()

这是实现:

manifest.xml

    <provider android:name=".utils.MyFileProvider"
            android:authorities="com.app.mtccrm.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <Meta-data android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
    </provider>

provider_paths.xml

    <?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external"
        path="." />
    <external-files-path
        name="external_files"
        path="." />

    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />
</paths>

method to create image_file

    public static File createImageFile(Context mContext,String name) throws IOException {
        
        String imageFileName = name.replaceAll(" ","_");
        File storageDir = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = new File(storageDir + File.separator + imageFileName + ".png");
        return image;
    }

method responsible for opening the camera

    private void openCamera() {
        if (ActivityCompat.checkSelfPermission(AddNewFileActivity.this,Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},REQUEST_PERMISSION_CODE_IMAGE);
        } else {
            Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            try {
                photoFile = CommonMethod.createImageFile(this,fileName);
            } catch (IOException ioe) {
                ioe.printstacktrace();
            }
            //Toast.makeText(this,""+photoFile.getAbsolutePath(),Toast.LENGTH_SHORT).show();
            if(photoFile != null ) {
                Uri photoURI = FileProvider.getUriForFile(this,BuildConfig.APPLICATION_ID + ".fileprovider",photoFile);
                captureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
                captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivityForResult(captureIntent,TAKE_PICTURE_CODE);
            }else {
                Toast.makeText(this,"Something went wrong while trying to create a file. Please try again",Toast.LENGTH_SHORT).show();
            }
        }
    }

onActivityResult()

    Toast.makeText(this,""+photoFile,Toast.LENGTH_SHORT).show();
        if (requestCode == TAKE_PICTURE_CODE && resultCode == RESULT_OK) {
            if(photoFile == null) {
                if(data != null) {
                   parsegalleryFile(data);
                }
            }else {
                setimage(Uri.parse(photoFile.getAbsolutePath()));
            }
        }

onActivityResult() 中的第一个 Toast 给了我 null 值。所以不知何故 photoFile 变量变得空了,但是我对原因一无所知。因为,从使用相同方法的相同活动中,录制视频和浏览图库工作得很好。我认为这可能与调用相机时活动暂停有关。所以,我加了这个

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState,@NonNull PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState,outPersistentState);
        Toast.makeText(this,Toast.LENGTH_SHORT).show();
        if(photoFile != null) {
            outState.putString("FileId",photoFile.getAbsolutePath());
        }
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Toast.makeText(this,""+savedInstanceState.getString("FileId"),Toast.LENGTH_SHORT).show();
        if(savedInstanceState.getString("FileId") != null) {
            if (photoFile == null) {
                photoFile = new File(savedInstanceState.getString("FileId"));
            }
        }
    }

不幸的是,保存和恢复状态方法没有被调用,这意味着活动没有被暂停。那么 photofile 变为 null 的原因可能是什么?我到底做错了什么?

感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)