【问题标题】:Get image from Gallery, but won't crop从图库中获取图像,但不会裁剪
【发布时间】:2014-03-13 12:43:06
【问题描述】:

我有这段代码可以从图库中检索图像

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 0);
    intent.putExtra("aspectY", 0);
    intent.putExtra("outputX", 360);
    intent.putExtra("outputY", 360);
 try
    {
        intent.putExtra("return-data", true);
        startActivityForResult(Intent.createChooser(intent, "Complete action using"), req_code);
    }
    catch(ActivityNotFoundException e)
    {
        // Do nothing for now
    }

但即使使用intent.putExtra("crop", "true");,在选择图像后,它也不会显示任何裁剪活动或其他任何东西......为什么?

【问题讨论】:

  • 在您的ImageView 中,将 scaleType 设置为 CenterCrop
  • 这实际上很有帮助!我会一直这样做,直到找到合适的方法来裁剪图像。
  • 好吧,如果你的图片很大,会出现outOfMemory 的问题,你最好按照谷歌的开发者指南或者使用库来裁剪大图。
  • 是的,至少我暂时有办法!
  • 好的,但你应该注意这一点。

标签: android android-intent crop


【解决方案1】:

因为它不应该。仅仅因为您在随机的 Intent 对象上添加了随机附加项,并不会神奇地强制第三方应用程序执行他们不执行的操作。

这里是the documentation for ACTION_GET_CONTENT。请注意,您在文档中列出的任何附加内容都没有。因此,没有第三方应用程序必然会期待这些额外功能。

Android 没有可供开发人员使用的内置图像裁剪功能。不过,有很多可用的图像裁剪库。

【讨论】:

    【解决方案2】:
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, SELECT_PHOTO);
    

    【讨论】:

      【解决方案3】:

      试试这个代码: (用于从图库中获取图像)

      Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                  photoPickerIntent.setType("image/*");
                  startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
                  dialog.dismiss();
      

      (从画廊中挑选图像后调用此)

      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          Bitmap bmp = null;
          if (resultCode == RESULT_OK) {
              try {
      
                  if (requestCode == GALLERY_REQUEST) {
                      // Gallery request result
                      mImageCaptureUri = data.getData();
      
                      TEMP_PHOTO_FILE_NAME = new File(
      
                      startCropImage();
      
              } catch (Exception e) {
                  e.printStackTrace();
                  File f = new File(getRealPathFromURI(mImageCaptureUri));
                  if (f.getName().startsWith(FILE_NAME)) {
                      if (f.exists())
                          f.delete();
                  }
              }
          }
      }
      
      private void startCropImage() {
      
      
          Intent intent = new Intent("com.android.camera.action.CROP");
          intent.setType("image/*");
      
        /**
           * Check if there is image cropper app installed.
           */
          List<ResolveInfo> list = getPackageManager().queryIntentActivities(
                  intent, 0);
      
          int size = list.size();
                  /**
           * If there is no image cropper app, display warning message
           */
          if (size == 0) {
      
              Toast.makeText(this, "Can not find image crop app",
                      Toast.LENGTH_SHORT).show();
      
              return;
          } else {
              /**
               * Specify the image path, crop dimension and scale
               */
              intent.setData(mImageCaptureUri);
      
              intent.putExtra("outputX", 256);
              intent.putExtra("outputY", 256);
              intent.putExtra("aspectX", 1);
              intent.putExtra("aspectY", 1);
              intent.putExtra("scale", true);
              intent.putExtra("return-data", true);
              /**
               * There is posibility when more than one image cropper app exist,
               * so we have to check for it first. If there is only one app, open
               * then app.
               */
      
              if (size == 1) {
                  Intent i = new Intent(intent);
                  ResolveInfo res = list.get(0);
      
                  i.setComponent(new ComponentName(res.activityInfo.packageName,
                          res.activityInfo.name));
      
                  startActivityForResult(i, CROP_FROM_CAMERA);
              }
      }
      

      【讨论】:

      【解决方案4】:
      protected void onActivityResult(int requestCode, int resultCode,
              Intent imageReturnedIntent) {
          super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
      
          switch (requestCode) {
          case SELECT_PHOTO: //Select photo == 1
              if (resultCode == RESULT_OK) {
                  try {
                      final Uri imageUri = imageReturnedIntent.getData();
                      Bitmap selectedImage = BitmapFactory
                              .decodeStream(getContentResolver().openInputStream(
                                      imageUri));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-17
        • 2012-12-14
        • 1970-01-01
        • 1970-01-01
        • 2018-09-23
        • 2015-01-01
        相关资源
        最近更新 更多