0

我有像这样裁剪图像的代码

crop.setOnClickListener(new OnClickListener(){   

            @Override
            public void onClick(View arg0) {       

                mImageCaptureUri = Uri.parse(path);

                doCrop();

如果按钮裁剪被点击,它将运行此代码来裁剪图像

protected void doCrop(){

           final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();

            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(mImageCaptureUri);
                intent.setType("image/*");
                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);
                //intent.putExtra("output", mImageCaptureUri);

                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, HASIL_CROP);
                } else {
                    for (ResolveInfo res : list) {
                        final CropOption co = new CropOption();

                        co.title    = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
                        co.icon     = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
                        co.appIntent= new Intent(intent);
                        co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

                        cropOptions.add(co);
                    }

                    CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);

                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("Choose Crop App");
                    builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
                        public void onClick( DialogInterface dialog, int item ) {
                            startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
                        }
                    });

                    builder.setOnCancelListener( new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel( DialogInterface dialog ) {

                            if (mImageCaptureUri != null ) {
                                getContentResolver().delete(mImageCaptureUri, null, null );
                                mImageCaptureUri = null;
                            }
                        }
                    } );

                    AlertDialog alert = builder.create();

                   alert.show();
                }    
            }

我从这段代码中得到 mImageCaptureUri

String oldFile = path;
            String imageTemp = "E"+System.currentTimeMillis();
            path = tempPath + imageTemp + ".jpg";   //alamat temporari image
            FileOutputStream ostream = new FileOutputStream(path);

            b.compress(CompressFormat.JPEG, 50, ostream);
            ostream.close();

            mImageCaptureUri = Uri.parse(path);

但是当我单击裁剪按钮时,它没有任何反应,Logcat 低于 10-24 16:47:21.669: INFO/ActivityManager(59): Starting activity: Intent { act=com.android.camera.action.CROP typ=image/* cmp =com.android.gallery/com.android.camera.CropImage(有附加功能)}

我认为是因为

intent.setData(mImageCaptureUri);
                    intent.setType("image/*");

但是如果我删除 intent.setType("image/*"),它会显示错误

10-24 16:51:24.549: ERROR/UriImage(345): got exception decoding bitmap 
10-24 16:51:24.549: ERROR/UriImage(345): java.lang.NullPointerException
10-24 16:51:24.549: ERROR/UriImage(345):     at com.android.camera.gallery.UriImage.getPFD(UriImage.java:67)
10-24 16:51:24.549: ERROR/UriImage(345):     at com.android.camera.gallery.UriImage.fullSizeBitmap(UriImage.java:93)
10-24 16:51:24.549: ERROR/UriImage(345):     at com.android.camera.gallery.UriImage.fullSizeBitmap(UriImage.java:86)
10-24 16:51:24.549: ERROR/UriImage(345):     at com.android.camera.gallery.UriImage.thumbBitmap(UriImage.java:120)
10-24 16:51:24.549: ERROR/UriImage(345):     at com.android.camera.CropImage.onCreate(CropImage.java:143)
10-24 16:51:24.549: ERROR/UriImage(345):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

任何人都可以帮忙吗?

谢谢

4

1 回答 1

0

@ya Lya 检查 mImageCaptureUri 是否为空。如果不为空,则只有您必须执行进一步的任务。我认为mImageCaptureUri 为空。

于 2012-10-24T10:18:41.430 回答