裁剪不显示图像而不工作 - android

时间:2013-03-31 18:17:19

标签: android crop

我正在尝试在我的Android平板电脑中裁剪,但它无法正常工作。

我使用的是Nexus 10。

嗯......这是我的裁剪代码:

private void doCrop(){
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(mImageCaptureUri, "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(mImageCaptureUri);        
        intent.putExtra("outputX", 648);
        intent.putExtra("outputY", 486);
        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 {
        }

    }
}

这是我的对话框,要求&#34;相机&#34;或&#34;图库&#34;:

private void startDialog() {
    AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
    myAlertDialog.setTitle("Selecione uma foto!");
    myAlertDialog.setMessage("Choose a source");

    myAlertDialog.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT, null);
            pictureActionIntent.setType("image/*");
            pictureActionIntent.putExtra("return-data", true);
            startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
        }
    });

    myAlertDialog.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            pictureActionIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            storagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmp/android.jpg";
            pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, storagePath);
            startActivityForResult(pictureActionIntent, CAMERA_PICTURE);
        }
    });
    myAlertDialog.show();
}    

这是我的onActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap bmp;
    if (resultCode == RESULT_OK){
        switch (requestCode){
        case GALLERY_PICTURE :
            mImageCaptureUri = data.getData();
            doCrop();
        break;
        case CROP_RESULT :
            Bundle extras = data.getExtras();
            if (extras != null) {               
                bmp = extras.getParcelable("data");
                bmp = Bitmap.createScaledBitmap(bmp, 648, 486, true);
                Drawable d = new BitmapDrawable(getResources(),bmp);
                btnTitular.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);
            }
            File f = new File(mImageCaptureUri.getPath());            
            if (f.exists()) f.delete();
            Intent inten3 = new Intent(this, CadTitularActivity.class);
            startActivity(inten3);
        break;
        case CAMERA_PICTURE :
            doCrop();
        break;
        }
    }        
}

以下是发生的事情:

当我从图库中抓取图像时,裁剪显示但是当我点击&#34;确定&#34;要裁剪图像,它什么都不做。

当我从相机中抓取图片时,它什么也没做。它只显示&#34;正在加载图片&#34;没有任何反应。

任何想法?

请记住:我使用的是Nexus 10.我已经听说过这种裁剪对所有设备都不起作用。

欢迎任何帮助!

谢谢!

1 个答案:

答案 0 :(得分:-1)

我认为问题是输出x和y必须将裁剪意图设置为256 x 256(或更小)。

所以改变代码:

intent.putExtra("outputX", 648);
intent.putExtra("outputY", 486);

到此:

intent.putExtra("outputX", 256);
intent.putExtra("outputY", 256);

它应该适合你。