2016-04-03 16 views
1

私はwebviewアプリを作成しています。 Webviewからファイル選択機能が必要です。私は正常にファイルチューザーを作成しましたが、ファイルマネージャーを開いてファイルを選択します。 これで、ユーザーがファイルを選択したときにカメラを開いておきます。Android LollipopのWebViewファイルセレクタからカメラを開く

public class MainActivity extends AppCompatActivity{ 

public static final String PAGE_URL = "http://192.168.94.1/fileupload/"; 

private static final int INPUT_FILE_REQUEST_CODE = 1; 
private static final int FILECHOOSER_RESULTCODE = 1; 
private static final String TAG = MainActivity.class.getSimpleName(); 
private WebView webView; 
private WebSettings webSettings; 
private ValueCallback<Uri> mUploadMessage; 
private Uri mCapturedImageURI = null; 
private ValueCallback<Uri[]> mFilePathCallback; 
private String mCameraPhotoPath; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    webView = (WebView) findViewById(R.id.activity_main_webview); 
    webSettings = webView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    webSettings.setLoadWithOverviewMode(true); 
    webSettings.setAllowFileAccess(true); 
    webView.setWebViewClient(new Client()); 
    webView.setWebChromeClient(new ChromeClient()); 
    if (Build.VERSION.SDK_INT >= 19) { 
     webView.setLayerType(View.LAYER_TYPE_HARDWARE, null); 
    } else if (Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT < 19) { 
     webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 
    } 
    webView.loadUrl(Constants.URL); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) { 
      super.onActivityResult(requestCode, resultCode, data); 
      return; 
     } 
     Uri[] results = null; 

     if (resultCode == Activity.RESULT_OK) { 
      if (data == null) { 
       if (mCameraPhotoPath != null) { 
        results = new Uri[]{Uri.parse(mCameraPhotoPath)}; 
       } 
      } else { 
       String dataString = data.getDataString(); 
       if (dataString != null) { 
        results = new Uri[]{Uri.parse(dataString)}; 
       } 
      } 
     } 
     mFilePathCallback.onReceiveValue(results); 
     mFilePathCallback = null; 
    } else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { 
     if (requestCode != FILECHOOSER_RESULTCODE || mUploadMessage == null) { 
      super.onActivityResult(requestCode, resultCode, data); 
      return; 
     } 
     if (requestCode == FILECHOOSER_RESULTCODE) { 
      if (null == this.mUploadMessage) { 
       return; 
      } 
      Uri result = null; 
      try { 
       if (resultCode != RESULT_OK) { 
        result = null; 
       } else { 
        result = data == null ? mCapturedImageURI : data.getData(); 
       } 
      } catch (Exception e) { 
       Toast.makeText(getApplicationContext(), "activity :" + e, 
         Toast.LENGTH_LONG).show(); 
      } 
      mUploadMessage.onReceiveValue(result); 
      mUploadMessage = null; 
     } 
    } 
    return; 
} 
private File createImageFile() throws IOException { 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 
    File storageDir = Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES); 
    File imageFile = File.createTempFile(
      imageFileName, /* prefix */ 
      ".jpg",   /* suffix */ 
      storageDir  /* directory */ 
    ); 
    return imageFile; 
} 

public class ChromeClient extends WebChromeClient { 
    public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams) { 
     if (mFilePathCallback != null) { 
      mFilePathCallback.onReceiveValue(null); 
     } 
     mFilePathCallback = filePath; 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
      File photoFile = null; 
      try { 
       photoFile = createImageFile(); 
       takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath); 
      } catch (IOException ex) { 
       Log.e(TAG, "Unable to create Image File", ex); 
      } 

      if (photoFile != null) { 
       mCameraPhotoPath = "file:" + photoFile.getAbsolutePath(); 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
         Uri.fromFile(photoFile)); 
      } else { 
       takePictureIntent = null; 
      } 
     } 
     Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); 
     contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); 
     contentSelectionIntent.setType("image/*"); 
     Intent[] intentArray; 
     if (takePictureIntent != null) { 
      intentArray = new Intent[]{takePictureIntent}; 
     } else { 
      intentArray = new Intent[0]; 
     } 
     Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); 
     chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); 
     chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); 
     chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); 
     startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE); 
     return true; 
    } 

    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { 
     mUploadMessage = uploadMsg; 
     File imageStorageDir = new File(
       Environment.getExternalStoragePublicDirectory(
         Environment.DIRECTORY_PICTURES) 
       , "ViewPeer"); 
     if (!imageStorageDir.exists()) { 
      imageStorageDir.mkdirs(); 
     } 
     File file = new File(
       imageStorageDir + File.separator + "IMG_" 
         + String.valueOf(System.currentTimeMillis()) 
         + ".jpg"); 
     mCapturedImageURI = Uri.fromFile(file); 

     final Intent captureIntent = new Intent(
       android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI); 
     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}); 
     startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); 
    } 

    public void openFileChooser(ValueCallback<Uri> uploadMsg) { 
     openFileChooser(uploadMsg, ""); 
    } 

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

public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { 
     webView.goBack(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

public class Client extends WebViewClient { 
    ProgressDialog progressDialog; 

    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (url.contains("mailto:")) { 
      view.getContext().startActivity(
        new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 
      return true; 
     } else { 
      view.loadUrl(url); 
      return true; 
     } 
    } 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
     if (progressDialog == null) { 
      progressDialog = new ProgressDialog(MainActivity.this); 
      progressDialog.setMessage("Loading..."); 
      progressDialog.show(); 
     } 
    } 

    public void onPageFinished(WebView view, String url) { 
     try { 
      if (progressDialog.isShowing()) { 
       progressDialog.dismiss(); 
       progressDialog = null; 
      } 
     } catch (Exception exception) { 
      exception.printStackTrace(); 
     } 
    } 
} 
} 

上記のコードは正常に動作しますが、イメージマネージャからファイルマネージャを開くことができます。しかし、私はどこからユーザーが直接アップロードする画像をクリックすることができますカメラを開くしたい。

私を助けてください:)

答えて

0

代わりIntent chooserIntent = new Intent(Intent.ACTION_CHOOSER);

使用ACTION_IMAGE_CAPTUREのこの

/*************************** Camera Intent Start ************************/ 

       // Define the file-name to save photo taken by Camera activity 

       String fileName = "Camera_Example.jpg"; 

       // Create parameters for Intent with filename 

       ContentValues values = new ContentValues(); 

       values.put(MediaStore.Images.Media.TITLE, fileName); 

       values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera"); 

       // imageUri is the current activity attribute, define and save it for later usage 

       imageUri = getContentResolver().insert(
         MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 

       /**** EXTERNAL_CONTENT_URI : style URI for the "primary" external storage volume. ****/ 


       // Standard Intent action that can be sent to have the camera 
       // application capture an image and return it. 

       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

       intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 

       intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 

       startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

      /*************************** Camera Intent End ************************/ 
+0

ようおかげで、あなたのコード –

+0

をしようとそれはカメラを開いているが、私は私のWebViewで画像を取得していないのですか?私はonActivityResultsのいくつかの変更を行う必要があると思います。私を助けてくれますか? –

+0

[カメラ](http://androidexample.com/Camera_Photo_Capture_And_Show_Captured_Photo_On_Activity_/index.php?view=article_discription&aid=77&aaid=101) – Kathi

関連する問題