【问题标题】:Web View Camera capture optionWeb View Camera 捕捉选项
【发布时间】:2013-10-22 12:25:11
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多