Created
          July 7, 2016 12:54 
        
      - 
      
- 
        Save KeithNdhlovu/57a06448ed5abb627031cdd17a494bad to your computer and use it in GitHub Desktop. 
    Lanches an Intent to either get image by using camera , or pick image by using Galery or any other applicable software
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | private Uri outputFileUri; | |
| private void openImageIntent() { | |
| // Determine Uri of camera image to save. | |
| final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator); | |
| root.mkdirs(); | |
| final String fname = Utils.getUniqueImageFilename(); | |
| final File sdImageMainDirectory = new File(root, fname); | |
| outputFileUri = Uri.fromFile(sdImageMainDirectory); | |
| // Camera. | |
| 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 intent = new Intent(captureIntent); | |
| intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); | |
| intent.setPackage(packageName); | |
| intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); | |
| cameraIntents.add(intent); | |
| } | |
| // Filesystem. | |
| final Intent galleryIntent = new Intent(); | |
| galleryIntent.setType("image/*"); | |
| galleryIntent.setAction(Intent.ACTION_GET_CONTENT); | |
| // Chooser of filesystem options. | |
| final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source"); | |
| // Add the camera options. | |
| chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{})); | |
| startActivityForResult(chooserIntent, YOUR_SELECT_PICTURE_REQUEST_CODE); | |
| } | |
| @Override | |
| protected void onActivityResult(int requestCode, int resultCode, Intent data) | |
| { | |
| if(resultCode == RESULT_OK) | |
| { | |
| if(requestCode == YOUR_SELECT_PICTURE_REQUEST_CODE) | |
| { | |
| final boolean isCamera; | |
| if(data == null) | |
| { | |
| isCamera = true; | |
| } | |
| else | |
| { | |
| final String action = data.getAction(); | |
| if(action == null) | |
| { | |
| isCamera = false; | |
| } | |
| else | |
| { | |
| isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); | |
| } | |
| } | |
| Uri selectedImageUri; | |
| if(isCamera) | |
| { | |
| selectedImageUri = outputFileUri; | |
| } | |
| else | |
| { | |
| selectedImageUri = data == null ? null : data.getData(); | |
| } | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment