2015-11-08 48 views
0

我有选择Image Galery和Camera Apps的问题。我遵循this tutorial。问题是Camera chooserIntent不能在Android M上启动(其他版本是启动),但Galery的意图是启动。那么如何解决它?Camera chooserIntent无法在Android M上启动

这是chooserIntent:

public static Intent getPickImageIntent(Context context) { 
     Intent chooserIntent = null; 

     List<Intent> intentList = new ArrayList<>(); 

     Intent pickIntent = new Intent(Intent.ACTION_PICK, 
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     takePhotoIntent.putExtra("return-data", true); 
     takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context))); 
     intentList = addIntentsToList(context, intentList, pickIntent); 
     intentList = addIntentsToList(context, intentList, takePhotoIntent); 

     if (intentList.size() > 0) { 
      chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), 
        context.getString(R.string.pick_image_intent_text)); 
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{})); 
     } 

    return chooserIntent; 
} 

并实现代码:

public class SimpleActivity { 
    private static final int PICK_IMAGE_ID = 234; // the number doesn't matter 

    public void onPickImage(View view) { 
    Intent chooseImageIntent = ImagePicker.getPickImageIntent(this); 
    startActivityForResult(chooseImageIntent, PICK_IMAGE_ID); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch(requestCode) { 
     case PICK_IMAGE_ID: 
      Bitmap bitmap = ImagePicker.getImageFromResult(this, resultCode, data); 
      // TODO use bitmap 
      break; 
     default: 
      super.onActivityResult(requestCode, resultCode, data); 
      break; 
    } 
    } 
} 

回答

0

对于您可以使用它打开相机意图。

static final int CAMERA_REQ_CODE = 1; 

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

fileUri = Uri.fromFile(yourFile); 

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

startActivityForResult(intent, CAMERA_REQ_CODE); 
0

编辑:运行权限只有一个问题。我想知道为什么你的应用程序在没有请求CAMERA许可时没有崩溃,我想清楚了。这是因为你从来没有打开相机:

chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), 
       context.getString(R.string.pick_image_intent_text)); 

您在intentList最后删除意图,又名takePhotoIntent。所以intentList只包含pickIntent。这就是为什么。


Android M自带Runtime Permission。在应用程序可以使用它们之前,某些权限需要由用户授予。在这种情况下,它们是CAMERAWRITE_EXTERNAL_STORAGE。所以你必须要求权限:

public void onPickImage(View view) { 
    askForCameraPermission(); 
} 

private void askForCameraPermission() { 
    // Neither CAMERA or WRITE_EXTERNAL_STORAGE is granted 
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) 
      != PackageManager.PERMISSION_GRANTED 
      && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.CAMERA) 
       || ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 

      // Show an expanation to the user *asynchronously* -- don't block 
      // this thread waiting for the user's response! After the user 
      // sees the explanation, try again to request the permission. 

      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setTitle("Permissions needed"); 
      builder.setMessage("Some explanations"); 
      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 

        // User understands the situation, now ask for permissions 
        ActivityCompat.requestPermissions(SimpleActivity.this, 
          new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
          PERMISSIONS_REQUEST_CAMERA); 
       } 
      }); 
      builder.show(); 

     } else { 

      // No explanation needed, we can request the permission. 

      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
        PERMISSIONS_REQUEST_CAMERA); 
     } 
    } else { 

     // Permissions are already granted before 
     Intent chooseImageIntent = ImagePicker.getPickImageIntent(this); 
     startActivityForResult(chooseImageIntent, PICK_IMAGE_ID); 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    switch (requestCode) { 
     case PERMISSIONS_REQUEST_CAMERA: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! 
       Intent chooseImageIntent = ImagePicker.getPickImageIntent(this); 
       startActivityForResult(chooseImageIntent, PICK_IMAGE_ID); 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 

       // Blame user 
       AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       builder.setTitle("Error"); 
       builder.setMessage("Y u do dis?!"); 
       builder.setPositiveButton("Nah", null); 
       builder.show(); 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 
+0

还没劳克相机:( –

0

使用Android M时,运行时权限发生了重大变化。为了使用相机,您必须在运行时授予权限。看看这个链接,它说明了如何处理权限申请流程:

New Android permissions