处理文件选择器 android 6.0 webview

Posted

技术标签:

【中文标题】处理文件选择器 android 6.0 webview【英文标题】:Handle file chooser android 6.0 webview 【发布时间】:2017-06-30 20:00:03 【问题描述】:

我是 android 新手,现在尝试开发 Web 应用程序。我花了几天时间让输入类型文件在 webview 中完美运行。直到我遵循并使用此代码来处理 webview 中的文件选择器。

enter link description here

当我将 AVD Nexus 与姜胡子一起使用并单击输入类型文件时,它可以显示两个选项(相机或画廊)。 问题是当我使用 6.0 的 AVD Nexus 并单击输入类型文件时,它会打开文件管理器而不显示选项相机或画廊。 每次用户单击输入类型文件时,我都想显示输入选项对话框。

我该怎么做?谢谢你的帮助。

【问题讨论】:

【参考方案1】:

为 android 5.0 及更高版本添加此功能

private static final int INPUT_FILE_REQUEST_CODE = 1;

private Context mContext=YourActivity.this; 
private static final int REQUEST_CAMERA = 111; 

// For Android 5.0

public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams)

    //  Log.d(TAG,"ShowFileChooser For Android 5.0 ");
    if (mFilePathCallback != null) 
        mFilePathCallback.onReceiveValue(null);
    
    mFilePathCallback = filePath;
    if (Build.VERSION.SDK_INT >= 23) 
        // Log.d(TAG,"ShowFileChooser For Android 5.0 SDK_INT>=23 chk permission");
        String[] PERMISSIONS = android.Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.CAMERA;
        if (!hasPermissions(mContext, PERMISSIONS)) 
            ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST_CAMERA);
         else 
            //Log.d(TAG,"ShowFileChooser For Android 5.0 in IF  SDK_INT>=23 permission grant");
            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 = Constant.create_file();
                    takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
                 catch (Exception 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);
        
     else 
        // Log.d(TAG,"ShowFileChooser For Android 5.0 in else SDK_INT>=23");
        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 = Constant.create_file();
                takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
             catch (Exception 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);
    
    // Double check that we don't have any existing callbacks
    return true;

检查棉花糖的权限

private static boolean hasPermissions(Context context, String... permissions) 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) 
        for (String permission : permissions) 
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) 
                return false;
            
        
    
    return true;

获取权限结果

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) 

        case REQUEST_CAMERA: 
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
                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 = Constant.create_file();
                        takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
                     catch (Exception 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);

                //reload my activity with permission granted or use the features what required the permission
             else 
                Patient_appointment.setWebChromeClient(new ChromeClient());
                Toast.makeText(mContext, "The app was not allowed to write to your storage", Toast.LENGTH_LONG).show();
            
        
    

【讨论】:

嗨,什么是 mContext? REQUEST_CAMERA 的价值?我的活动名称是 InventoryDataEditor。 上下文 mContext=InventoryDataEditor.this;私有静态最终 int REQUEST_CAMERA = 111; 在授予权限后,它仍然显示 Toast 和 Toast.makeText(mContext, "The app was not allowed to write to your storage", Toast.LENGTH_LONG).show();【参考方案2】:

如果想要在 API 21 以上的 Web 视图客户端中使用文件上传功能,请点击下面的链接,它比上面的答案要简单得多:-

https://github.com/anthonycr/Lightning-Browser/issues/253

【讨论】:

以上是关于处理文件选择器 android 6.0 webview的主要内容,如果未能解决你的问题,请参考以下文章

Android 6.0 运行时权限处理

Android - WebView 在简历中死亡

Android 6.0 处于打盹模式时如何让闹钟管理器工作?

Android 文件选择器支持pdf,txt apk等文件格式查询

Android 文件选择器支持pdf,txt apk等文件格式查询

Android 6.0 运行时权限处理完全解析