【问题标题】:Crop saved Image using com.android.camera.action.CROP on android在 android 上使用 com.android.camera.action.CROP 裁剪保存的图像
【发布时间】:2012-08-29 20:13:22
【问题描述】:

我已经阅读了很多关于此的问题,但我仍然无法使用此代码...也许任何人都可以纠正我的代码...我想从我知道使用 com.android.camera 的位置的文件中裁剪图像。 action.CROP 像这样...

    mImageCaptureUri = Uri.fromFile(f);
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setType("image/*");

    intent.setData(mImageCaptureUri); 
    intent.putExtra("crop", true);
    intent.putExtra("outputX", 200);
    intent.putExtra("outputY", 200);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);

    Bundle extras = intent.getExtras();

    if (extras != null) {               
        Bitmap photo = extras.getParcelable("intent");

        tampilan.setImageBitmap(photo);
    }

    File f = new File(mImageCaptureUri.getPath());            

    if (f.exists()) f.delete();

但是当我运行代码时,什么都没有发生... T.T 谁能帮帮我??

【问题讨论】:

    标签: android image android-intent crop android-image


    【解决方案1】:

    我已经成功修改了我的代码和它的工作,这是我的代码..

    Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setType("image/*");
        List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
        int size = list.size();
        if (size == 0) {            
            Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
    
            return;
        } else {
            intent.setData(selectImageUri);        
            intent.putExtra("outputX", 300);
            intent.putExtra("outputY", 300);
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            intent.putExtra("scale", true);
            intent.putExtra("return-data", true);
            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_RESULT);
            } else {
    
            }
    
        }
    

    还有这样的activityResult

    if (resultCode == RESULT_OK){
            switch (requestCode){
            case SELECT_PICTURE :
                selectImageUri = data.getData();
                doCrop();
            break;
            case CROP_RESULT :
                Bundle extras = data.getExtras();
    
                if (extras != null) {               
                    bmp = extras.getParcelable("data");
                    temporary.setBitmap(bmp);
    
                }
                File f = new File(selectImageUri.getPath());            
    
                if (f.exists()) f.delete();
                Intent inten3 = new Intent(this, tabActivity.class);
                startActivity(inten3);
            break;
            case CAMERA_IMAGE :
                doCrop();
            break;
            }
        }
    

    也许它有用:D

    【讨论】:

    • “com.android.camera.action.CROP”有什么作用?如果我已经有来自“android.provider.MediaStore.ACTION_IMAGE_CAPTURE”或“Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI”的图像怎么办?我需要从哪里开始遵循您的代码?
    【解决方案2】:

    使用这种意图是非常冒险的。它甚至不是文档的一部分。 Here's 更多解释为什么这是一件有风险的事情。 我建议使用第三方库进行裁剪,例如this one

    【讨论】:

      【解决方案3】:

      然后启动activity,使用这个

      startActivityForResult(intent, 1);  
      

      然后使用以下方法获取裁剪后的图像

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              if (resultCode != RESULT_OK)return;
      
              switch (requestCode) {
      
                  case 1:         
                      Bundle extras = data.getExtras();
                      if (extras != null) {               
                          photo = extras.getParcelable("data");               
      
      
                              tampilan.setBitmap(photo);
      break;
      }
      }
      }
      

      【讨论】:

      • 是的,我已经添加了该代码,但仍然没有发生任何事情并给出黑色位图.. 裁剪过程仍然没有显示..
      • 我尝试了代码,,但是当它运行 startActivityForResult 时,它给出了一个强制关闭错误,,你知道它为什么会发生吗??
      • @adi.zean 如果您有自己的解决方案.. 不要编辑您的问题来发布您的解决方案.. 回答您自己的问题并接受它。愿它对其他人有所帮助
      • 我对此感到抱歉..好的,我会编辑并回答我的问题..:D
      【解决方案4】:
          try {
              Intent intent = new Intent("com.android.camera.action.CROP");
              intent.setDataAndType(mPhotoUri, "image/*");
              intent.putExtra("crop", "true");
              Integer altura = documento.getAltura();
              Integer largura = documento.getLargura();
              if (altura != null && largura != null) {
                  intent.putExtra("aspectY", altura);
                  intent.putExtra("aspectX", largura);
              }
              File file = imagemProcessor.getNewFile();
              mCropUri = Uri.fromFile(file);
              intent.putExtra(MediaStore.EXTRA_OUTPUT, mCropUri);
              intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
              startActivityForResult(intent, REQUEST_IMAGE_CROP);
          } catch (ActivityNotFoundException e) {
              Context context = getBaseContext();
              Toast toast = Toast.makeText(context, "Your device doesn't support the crop action!", Toast.LENGTH_SHORT);
              toast.show();
          } catch (IOException e) {
              Context context = getBaseContext();
              Toast toast = Toast.makeText(context, "Fail to create file!", Toast.LENGTH_SHORT);
              toast.show();
          }
      

      【讨论】:

      • 不要只是编写代码,而是尝试详细说明您的解决方案。
      • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。一个正确的解释would greatly improve 其长期价值,通过展示为什么这是解决问题的好方法,并将使其对未来有其他类似问题的读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
      猜你喜欢
      • 2012-08-30
      • 1970-01-01
      • 2014-11-03
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多