当前位置: 首页 > 知识库问答 >
问题:

在Nexus、Android WebView中无法从摄像头上传照片

公西兴业
2023-03-14

我在Android webview上工作。我在从Nexus 5中的Camera动作上传图像时遇到了问题。在这里,相机打开,我点击图像,但然后什么也没发生,图像无法上传。但它可以很好地与文档或图库。有很多解决方案和这个。但我无法解决我的问题。可以做些什么来解决这个问题?

在我的menifest文件中,我包括:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在我的活动中,我包括:

private static final int INPUT_FILE_REQUEST_CODE = 1;
private static final int FILECHOOSER_RESULTCODE = 1;
private static final String TAG = MainActivity.class.getSimpleName();
private WebView webView;
private ValueCallback<Uri> mUploadMessage;
private Uri mCapturedImageURI = null;
private ValueCallback<Uri[]> mFilePathCallback;
private String mCameraPhotoPath;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == 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 (mCameraPhotoPath != null) {
                    results = new Uri[]{Uri.parse(mCameraPhotoPath)};
                }
            }
            else {
                String dataString = data.getDataString();
                if (dataString != null) {
                    results = new Uri[]{Uri.parse(dataString)};
                }
            }
        }

        mFilePathCallback.onReceiveValue(results);
        mFilePathCallback = null;

    } else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
        if (requestCode != FILECHOOSER_RESULTCODE || mUploadMessage == null) {
            super.onActivityResult(requestCode, resultCode, data);
            return;
        }

        if (requestCode == FILECHOOSER_RESULTCODE) {

            if (null == this.mUploadMessage) {
                return;

            }

            Uri result = null;

            try {
                if (resultCode != RESULT_OK) {

                    result = null;

                } else {

                    // retrieve from the private variable if the intent is null
                    result = data == null ? mCapturedImageURI : data.getData();
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "activity :" + e,
                        Toast.LENGTH_LONG).show();
            }

            mUploadMessage.onReceiveValue(result);
            mUploadMessage = null;

        }
    }

    return;
}

对于我的WebChromeClient,我包括:

public class MyWebChromeClient extends WebChromeClient{
// For Android 5.0
    public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams) {
        // Double check that we don't have any existing callbacks
        if (mFilePathCallback != null) {
            mFilePathCallback.onReceiveValue(null);
        }
        mFilePathCallback = filePath;

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
                takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
            } catch (IOException ex) {
                // Error occurred while creating the File
                Log.e(TAG, "Unable to create Image File", ex);
            }

            // Continue only if the File was successfully created
            if (photoFile != null) {
                mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
            } else {
                takePictureIntent = null;
            }
        }

        Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
        contentSelectionIntent.setType("image/*");

        Intent[] intentArray;
        if (takePictureIntent != null) {
            intentArray = new Intent[]{takePictureIntent};
        } else {
            intentArray = new Intent[0];
        }

        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;

    }

    // openFileChooser for Android 3.0+
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {

        mUploadMessage = uploadMsg;
        // Create AndroidExampleFolder at sdcard
        // Create AndroidExampleFolder at sdcard

        File imageStorageDir = new File(
                Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES)
                , "Photo Folder");

        if (!imageStorageDir.exists()) {
            // Create AndroidExampleFolder at sdcard
            imageStorageDir.mkdirs();
        }

        // Create camera captured image file path and name
        File file = new File(
                imageStorageDir + File.separator + "IMG_"
                        + String.valueOf(System.currentTimeMillis())
                        + ".jpg");
        Log.d("File", "File: " + file);
        mCapturedImageURI = Uri.fromFile(file);

        // Camera capture image intent
        final Intent captureIntent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);

        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");

        // Create file chooser intent
        Intent chooserIntent = Intent.createChooser(i, "Image Chooser");

        // Set camera intent to file chooser
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS
                , new Parcelable[] { captureIntent });

        // On select image call onActivityResult method of activity
        startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);


    }

    // openFileChooser for Android < 3.0
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        openFileChooser(uploadMsg, "");
    }

    //openFileChooser for other Android versions
    public void openFileChooser(ValueCallback<Uri> uploadMsg,
                                String acceptType,
                                String capture) {

        openFileChooser(uploadMsg, acceptType);
    }
}

这里可以做些什么,以便图像通过相机动作上传?

共有2个答案

濮阳繁
2023-03-14
匿名用户

将android:requestLegacyExternalStorage=“true”添加到清单中的应用程序标记中

邬飞捷
2023-03-14

我通过在onActivityResult方法中更改以下代码行来解决我的Camera问题:

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

我添加以下内容:if(data==null | | data.getDataString()==null){…}

 类似资料:
  • 我有一个问题与我的代码,我必须上传图像到Firebase存储,我需要的图像来自画廊和相机,从画廊的图像是好的,但来自相机的图像给问题,图像加载在ImageView和被发送到数据库是黑色的。有人知道如何解决这个问题吗,或者你知道任何其他加载图像的方法吗? 来自画廊 从相机

  • 我正在开发一个简单的博客应用程序,允许用户将照片从手机图库和描述上传到Firebase服务器。我正在尝试修改我的当前项目,以允许用户从照相机捕获照片并将其上载到firebase服务器。 目前,我能够将我捕获的图像显示到Image按钮中,但是我无法将我的图像发布到Firebase服务器(提交发布按钮不会对我的点击功能做出反应)。 我怀疑我的startPosting()函数中有错误,或者我没有正确编码

  • 我正在开发一个web应用程序,它可以浏览和拍摄本地照片,我还想通过相机拍摄图像。我使用下面的代码,我可以捕捉设备摄像头。 现在,我想获得图像和onchangeevent,转换为base64,并希望在该页面中显示。 好心帮我伙计们!

  • 我正在尝试用相机拍照并上传到Firebase。我使用AlertDialog询问用户是否想要使用相机或从图库中选择图像。我可以用相机拍照,但是当我试图上传图像时,它说找不到图像。 以下是我的图像选择方法: 下面是我上传图片的方法: 提前感谢任何帮助的朋友。

  • 我正在尝试使用GStreamer将RTMP/RTSP流连接到v4l2loopback虚拟设备。 工程1-RTMP至AutoVideoSink,sudo gst-launch-1.0 rtspsrc位置=rtsp://192.168.xxx.xxx/live/av0 ! decodebin!自动视频接收器sudo gst-launch-1.0 rtmpsrc位置=rtmp://192.168.xxx

  • 后摄像头工作正常,但是,当我们从后摄像头切换到前摄像头时,它会崩溃(在使用MediaCorder录制视频的情况下)....它显示了我在日志中显示的错误!! 下面是我的代码: 对于初始化,我使用