0

我想在 webview 中上传文件。当我单击 android 版本高于 3.0 的设备的选择按钮时,选择器中没有显示录音机选项。这是我的代码

public void openFileChooser(ValueCallback<Uri> uploadMsg) {

        String path = Environment.getExternalStorageDirectory().toString();
        File file = new File(path, "temp.jpg");
        file.delete();
        mUploadMessage = uploadMsg;  
        Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");
        Intent openInChooser=Intent.createChooser(i,"File Chooser");
        Intent value[]=new Intent[2];
        value[0]= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        value[0].addCategory(Intent.CATEGORY_OPENABLE);

        Intent fileExplorer= new Intent(MainActivity.this,DirectoryBrowser.class);
        fileExplorer.setComponent(new ComponentName(MainActivity.this, DirectoryBrowser.class));
        value[1] = new LabeledIntent(fileExplorer, "com.app.insta7", "File Explorer", R.drawable.folder);
        openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, value);
        MainActivity.this.startActivityForResult(openInChooser, FILECHOOSER_RESULTCODE);
    }  

如果我添加这一行并将意图数组大小增加到 3

value[2] = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);

对于版本低于 3.0 的设备,选择器显示选项两次。如果您有任何问题,请建议解决此问题。

4

1 回答 1

0

这个令人讨厌的无证函数已经改变了原型几次。当我们想要支持文件上传时,我们使用此代码:

// For Android 4.1+
public void openFileChooser(ValueCallback<Uri> uploadFileCallback, String acceptType, String capture) {
    Log.i(TAG, "Opening file chooser for type '" + acceptType + "', capture '" + capture + "'");
    // Add your file picker code here!
}

// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadFileCallback, String acceptType) {
    Log.i(TAG, "Opening legacy file chooser for type '" + acceptType + "'");
    openFileChooser(uploadFileCallback, acceptType, "");
}

// For Android < 3.0
public void openFileChooser(ValueCallback<Uri> uploadFileCallback) {
    Log.i(TAG, "Opening very legacy file chooser");
    openFileChooser(uploadFileCallback, "");
}
于 2013-11-15T11:46:47.533 回答