对于文件选择器openFileChooser()方法,不调用Kitkat 4.4的WebChromeClient

时间:2013-12-31 11:19:31

标签: javascript android

我有一个hi-bride应用程序,其中一个html页面有文件选择器,我想在Android webview中加载该页面。

此选择器在设备浏览器中运行良好,但在webview中无效。

为了支持这一点,我使用的是WebChromeClient的一个隐藏方法,如下所示

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){  
            /**updated, out of the IF **/
            mUploadMessage = uploadMsg;
            /**updated, out of the IF **/
            if(boolFileChooser){ //Take picture from filechooser
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
                i.addCategory(Intent.CATEGORY_OPENABLE);  
                i.setType("image/*");  
                startActivityForResult( Intent.createChooser( i, "Pick File.." ), FILECHOOSER_RESULTCODE );  
            } else { //Take photo and upload picture
                Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
                photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
                if(photo.exists())
                    photo.delete();
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
                mCapturedImageURI = Uri.fromFile(photo);
                startActivityForResult(cameraIntent, CAMERAREQUEST_RESULTCODE);
            }
        }
    // Per Android < 3.0
        public void openFileChooser(ValueCallback<Uri> uploadMsg){
            openFileChooser(uploadMsg, "");
        }
        //Aftre
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            openFileChooser(uploadMsg, "");
        }

直到4.3工作正常,但从4.4开始这个方法没有被调用。 他们说 https://code.google.com/p/android/issues/detail?id=62220 已被删除。

有谁知道任何替代方式。请让我知道您的帮助将非常感谢

3 个答案:

答案 0 :(得分:4)

4.3之后没有办法 openFileChooser 方法,因为google删除了这个方法,他们将在下一个版本中提出处理此文件选择器的其他方法(由Google工程师确认)。

我转向混合架构并为文件选择器编写本机函数。

答案 1 :(得分:1)

在Android 5.0中,他们引入了onShowFileChooser(),您可以使用它在webview中使用输入表单字段并启动文件选择器以从设备中选择图像和文件。

答案 2 :(得分:-1)

Bitmap bitmap;

private static final int READ_REQUEST_CODE = 42;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

    // Filter to only show results that can be "opened", such as a
    // file (as opposed to a list of contacts or timezones)
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    // Filter to show only images, using the image MIME data type.
    // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
    // To search for all documents available via installed storage providers,
    // it would be "*/*".
    intent.setType("image/*");

    startActivityForResult(intent, READ_REQUEST_CODE);

}


@Override
public void onActivityResult(int requestCode, int resultCode,
    Intent resultData) {

    // The ACTION_OPEN_DOCUMENT intent was sent with the request code
    // READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
    // response to some other intent, and the code below shouldn't run at all.

    if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        // The document selected by the user won't be returned in the intent.
        // Instead, a URI to that document will be contained in the return intent
        // provided to this method as a parameter.
        // Pull that URI using resultData.getData().
        Uri uri = null;
        if (resultData != null) {
            uri = resultData.getData();

            try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             ImageView my_img_view = (ImageView ) findViewById (R.id.uploadlayout2);
             my_img_view.setImageBitmap(bitmap);                                        
        }
    }
}