0

我是 android 新手,我已经开发了一个使用 web 视图的应用程序,现在当我上传任何图像时,它会将我带到画廊我想要一个弹出窗口,当它显示两个标题为“画廊”和“相机”的按钮时,如果我单击图库,它会将我带到图库以获取图像,如果我单击相机,它将允许我捕获并上传该图像。我还与大家分享我当前的代码。

public class MainActivity extends Activity {

MainActivity activity = this;
WebView webView;
private ValueCallback<Uri> mUploadMessage;  
private final static int FILECHOOSER_RESULTCODE=1;  

@Override  
protected void onActivityResult(int requestCode, int resultCode,  
                                   Intent intent) {  
 if(requestCode==FILECHOOSER_RESULTCODE)  
 {  
  if (null == mUploadMessage) return;  
           Uri result = intent == null || resultCode != RESULT_OK ? null  
                   : intent.getData();  
           mUploadMessage.onReceiveValue(result);  
           mUploadMessage = null;  

 }  
}
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.activity_main);
    webView = (WebView) findViewById(R.id.webview1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSupportZoom(true);
    webView.setWebChromeClient(new WebChromeClient() {


            public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType ) { 

             mUploadMessage = uploadMsg;  
             Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
             i.addCategory(Intent.CATEGORY_OPENABLE);  
             i.setType("video/*, image/*");  
             MainActivity.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);  

            }  

           public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { 

            mUploadMessage = uploadMsg;  
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
            i.addCategory(Intent.CATEGORY_OPENABLE);  
            i.setType("video/*, image/*");  
            MainActivity.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);  

           }

               public void openFileChooser(ValueCallback<Uri> uploadMsg) {  

                mUploadMessage = uploadMsg;  
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
                i.addCategory(Intent.CATEGORY_OPENABLE);  
                i.setType("video/*, image/*");  
                MainActivity.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);  

               }  

        public void onProgressChanged(WebView view, int progress)
        {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);

            if(progress == 100)
                activity.setTitle(R.string.app_name);
        }
    });

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    });

    webView.loadUrl("URL");
}
}
4

1 回答 1

0

不确定您是否找到了您正在寻找的答案,但这就是我最终要解决的同一问题。

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            Log.i(TAG, "Opening file chooser for type '" + acceptType + "', capture '" + capture + "'");
            // Allows for the camera option within the select window when file select button is clicked
            File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Citizen Mobile");
            // Create the storage directory if it does not exist
            if (! imageStorageDir.exists()){
                imageStorageDir.mkdirs();                  
            }
            File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");  
            imageUri = Uri.fromFile(file); 


            final List<Intent> cameraIntents = new ArrayList<Intent>();
            final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            final PackageManager packageManager = getPackageManager();
            final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
            for(ResolveInfo res : listCam) {
                final String packageName = res.activityInfo.packageName;
                final Intent i = new Intent(captureIntent);
                i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                i.setPackage(packageName);
                i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                cameraIntents.add(i);

            }
            //Allows for the use of the Galley or Pictures folders to select a file to upload
            mUploadMessage = uploadMsg; 
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
            i.addCategory(Intent.CATEGORY_OPENABLE);  
            i.setType("image/*"); 
            Intent chooserIntent = Intent.createChooser(i,"Image Chooser");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{captureIntent});
            MainActivity.this.startActivityForResult(chooserIntent,  FILECHOOSER_RESULTCODE);
        }

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

        // Required to open the file chooser options in Android < 3.0
        @SuppressWarnings("unused")
        public void openFileChooser(ValueCallback<Uri> uploadFileCallback) {
            Log.i(TAG, "Opening very legacy file chooser");
            openFileChooser(uploadFileCallback, "");
        }
于 2013-12-31T14:52:58.633 回答