Web View相机捕获选项

时间:2013-10-22 12:25:12

标签: android webview android-imageview android-camera-intent

我是Android的新手,我已经使用网页浏览器开发了一个应用程序,现在当我上传任何图像时它带我去画廊我想要一个弹出窗口,它显示我有两个标题为“画廊”和“相机”的按钮因此,如果我点击图库,它将带我去图库的画廊,如果我点击相机,它将允许我捕获并上传该图像。我也在和你们分享我现在的代码。

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");
}
}

1 个答案:

答案 0 :(得分: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, "");
        }