首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在我单击相机意图屏幕上的“是”图标后,Web视图显示小的崩溃图像图标

在我单击相机意图屏幕上的“是”图标后,Web视图显示小的崩溃图像图标
EN

Stack Overflow用户
提问于 2020-04-10 06:31:23
回答 1查看 166关注 0票数 0

我正在创建一个应用程序,在网页视图中加载一个URL。我已经创建了图像选择器来开始从画廊和相机中选择照片的活动。

( A)从画廊中挑选出的图像工作得很好。( B)当我通过摄像机捕捉图像时,它在web视图中显示崩溃的小图像图标。

注意:当我在拍摄图像后的相机屏幕上停留超过5-8秒时,相机照片在网络视图上显示得很完美。但这应该是工作,即使我按下是按钮后,迅速捕获的图像。

这里是我用于set WebChromeClient的web视图代码

代码语言:javascript
复制
        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public boolean onShowFileChooser(
                    WebView webView, ValueCallback<Uri[]> newFilePathCallback,
                    FileChooserParams fileChooserParams) {

                if (filePathCallback != null) {
                    filePathCallback.onReceiveValue(null);
                }
                filePathCallback = newFilePathCallback;

                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    Log.d("Mainn", "onShowFileChooser()");
                    // Create the File where the photo should go
                    File photoFile = null;
                    try {
                        photoFile = createImageFile();
                    } catch (IOException e) {
                        Log.d("Mainn", "onShowFileChooser().. e : " + e.getLocalizedMessage());
                        e.printStackTrace();
                    }

                    Log.d("Mainn", "onShowFileChooser() 4");
                    // Continue only if the File was successfully created
                    if (photoFile != null) {
                        Log.d("Mainn", "onShowFileChooser() 5");
                        cameraPhotoPath = "file:" + photoFile.getAbsolutePath();
                        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                                Uri.fromFile(photoFile));
                    } else {
                        Log.d("Mainn", "onShowFileChooser() 6");
                        takePictureIntent = null;
                    }
                }
                Log.d("Mainn", "onShowFileChooser() 7");
                Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
                contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
                contentSelectionIntent.setType("image/*");

                Intent[] intentArray;
                if (takePictureIntent != null) {
                    Log.d("Mainn", "onShowFileChooser() 8");
                    intentArray = new Intent[]{takePictureIntent};
                } else {
                    Log.d("Mainn", "onShowFileChooser() 9");
                    intentArray = new Intent[0];
                }

                Log.d("Mainn", "onShowFileChooser() 10");
                Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
                chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
                chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);

                startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);

                return true;
            }
        });
    }

onActivityResult()代码在这里

代码语言:javascript
复制
    @Override
    public void onActivityResult(int requestCode, int resultCode, final Intent data) {
        if (requestCode != INPUT_FILE_REQUEST_CODE || filePathCallback == null) {
            super.onActivityResult(requestCode, resultCode, data);
            return;
        }

        Uri[] results = null;

        // Check that the response is a good one
        if (resultCode == Activity.RESULT_OK) {
            if (data == null) {
                // If there is not data, then we may have taken a photo
                if (cameraPhotoPath != null) {
                    results = new Uri[]{Uri.parse(cameraPhotoPath)};
                }
            } else {
                String dataString = data.getDataString();
                if (dataString != null) {
                    results = new Uri[]{Uri.parse(dataString)};
                }
            }
        }

        filePathCallback.onReceiveValue(results);
        filePathCallback = null;

    }
EN

回答 1

Stack Overflow用户

发布于 2020-04-10 08:43:17

使用FileProvider而不是Uri.fromFile()。这将解决这个问题。

代码语言:javascript
复制
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Uri uri = FileProvider.getUriForFile(getActivity(), getActivity().getPackageName(), photoFile);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61135119

复制
相关文章

相似问题

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