2015-01-29 3 views
3

WebView에서 입력 한 파일을 사용하여 카메라를 사용해야하는 앱을 개발 중입니다.Android에서 일부 카메라 의도 제외

그래서이 코드는 내가 작성하고 Google 카메라에서 작동됩니다. 그것은 이미지 업로드 작업 또는 구글 카메라를 사용하여 사진을 복용한다

private Uri imageUri; 
@Override 
protected void onActivityResult(int requestCode, int resultCode, 
            Intent intent) { 
    if (Build.VERSION.SDK_INT >= 21){ 
     if(requestCode != FILECHOOSER_RESULTCODE || mFilePathCallback == null) { 
      super.onActivityResult(requestCode, resultCode, intent); 
      return; 
     } 

     Uri[] results = null; 

     // Check that the response is a good one 
     if(resultCode == Activity.RESULT_OK) { 
      if(intent == null) { 
       // If there is not data, then we may have taken a photo 
       if(mCameraPhotoPath != null) { 
        results = new Uri[]{Uri.parse(mCameraPhotoPath)}; 
       } 
      } else { 
       String dataString = intent.getDataString(); 
       if (dataString != null) { 
        results = new Uri[]{Uri.parse(dataString)}; 
       } 
      } 
     } 

     mFilePathCallback.onReceiveValue(results); 
     mFilePathCallback = null; 
     return; 
    }else{ 
      if(requestCode==FILECHOOSER_RESULTCODE) 
      { 
       if (null == this.mUploadMessage) { 
        return; 
       } 

       Uri result; 
       if (resultCode != RESULT_OK) { 
        result = null; 
       } else { 
        result = intent == null ? this.imageUri : intent.getData(); // retrieve from the private variable if the intent is null 
       } 

       this.mUploadMessage.onReceiveValue(result); 
       this.mUploadMessage = null; 
      } 
    } 
} 

: 내 webchromeclient

webView.setWebChromeClient(new WebChromeClient() 
    { 

      //The undocumented magic method override 
      //Eclipse will swear at you if you try to put @Override here 
     // For Android 3.0+ 
     @SuppressWarnings("unused") 
     public void openFileChooser(ValueCallback<Uri> uploadMsg) { 

      mUploadMessage = uploadMsg; 
      File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp"); 
      // 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); 

      } 


      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, cameraIntents.toArray(new Parcelable[]{})); 
      MainActivity.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); 

     } 

     //For Android 4.1 
     @SuppressWarnings("unused") 
     public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){ 
      mUploadMessage = uploadMsg; 
      File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp"); 
      // 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); 

      } 


      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, cameraIntents.toArray(new Parcelable[]{})); 
      MainActivity.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); 

     } 

     //For Android 3.0+ 
     @SuppressWarnings("unused") 
     public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){ 
      mUploadMessage = uploadMsg; 
      File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp"); 
      // 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); 

      } 


      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, cameraIntents.toArray(new Parcelable[]{})); 
      MainActivity.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); 

     } 

     //For Android 5.0+ 
     public boolean onShowFileChooser(
       WebView webView, ValueCallback<Uri[]> filePathCallback, 
       WebChromeClient.FileChooserParams fileChooserParams) { 

      // Double check that we don't have any existing callbacks 
      if(mFilePathCallback != null) { 
       mFilePathCallback.onReceiveValue(null); 
      } 
      mFilePathCallback = filePathCallback; 

      // Set up the take picture intent 
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) { 
       // Create the File where the photo should go 
       File photoFile = null; 
       try { 
        photoFile = createImageFile(); 
        takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath); 
       } catch (IOException ex) { 
        // Error occurred while creating the File 
        Log.e(tag, "Unable to create Image File", ex); 
       } 

       // Continue only if the File was successfully created 
       if (photoFile != null) { 
        mCameraPhotoPath = "file:" + photoFile.getAbsolutePath(); 
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
          Uri.fromFile(photoFile)); 
       } else { 
        takePictureIntent = null; 
       } 
      } 

      // Set up the intent to get an existing image 
      Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); 
      contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); 
      contentSelectionIntent.setType("image/*"); 

      // Set up the intents for the Intent chooser 
      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, MainActivity.FILECHOOSER_RESULTCODE); 

      return true; 
     } 
    }); 

에서

다음이 내 activityresult입니다.

내 질문은 입력 작업을 수행 할 때 일부 카메라 의도를 제외하는 방법입니까?

예를 들어 목록에서 카메라 360이나 retrica를 제거하고 싶습니다. 어떻게해야합니까? http://i.imgur.com/n3Ni3wq.jpg

답변

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

     } 

한이 사람 :

for(ResolveInfo res : listCam) { 
       final String packageName = res.activityInfo.packageName; 
       final Intent i = new Intent(captureIntent); 
       if(packageName.equals("ihate.this.package")||packageName.equals("begone.unwanted.package")){ 
       Log.i("camera", res.activityInfo.packageName+" blocked!"); 
       }else{ 
       i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); 
       captureIntent.setPackage(packageName); 
       i.setPackage(packageName); 
       i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
        cameraIntents.add(i); 
       } 

      } 

패키지 이름 그냥 res.activityInfo.packageName

당신은 할 수 있습니다 로그 알고 싶다면 android 5.0과 동일한 접근 방식입니다.

0

setPackage으로 사용 가능한 앱을 제한 할 수 있습니다.

+0

setPackage는 허용 된 응용 프로그램에만 제한됩니다. 필요한 것은 일부 패키지를 제외하는 것입니다. 다른 oems에 대한 카메라 애플 리케이션에 내장 된 너무 많은, 그래서 그들 모두를 예측할 수 없습니다. 그게 왜 내가 원하지 않는 카메라 앱을 제외해야한다고 생각하니. – hhnrui

0

아니요, 수행하려는 작업이 지원되지 않습니다. setPackage를 사용하여 하나의 특정 앱으로 제한하거나 패키지를 null로 유지하고 가능한 모든 앱이 의도를 처리하도록 허용 할 수 있습니다.