【问题标题】:how to extract URI from the crop file?如何从裁剪文件中提取 URI?
【发布时间】:2014-05-14 12:10:18
【问题描述】:

我可以从相机或图库中裁剪图片并将其放在ImageView 上,并且可以正确显示。 但是当我尝试将图片上传到服务器时,上传的是原始图片而不是裁剪后的图片。 如何获取裁剪图片的 URI?我想我在CROP_FROM_CAMERA 中遗漏了一些东西:

请帮忙。非常感谢。

我的crop和onstartactivity如下:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //   super.onActivityResult(requestCode, resultCode, data);
         if (resultCode != RESULT_OK) return;

            switch (requestCode) {
                case PICK_FROM_CAMERA:
                    doCrop();

                    break;

                case PICK_FROM_FILE: 
                    mImageCaptureUri = data.getData();

                    doCrop();


                    break;          

                case CROP_FROM_CAMERA:          
                    Bundle extras = data.getExtras();


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

                        mImageView.setImageBitmap(photo);
                    //    mImageCaptureUri = mImageView.;

                    }



                 //   File f = new File(mImageCaptureUri.getPath());            
                   // mImageCaptureUri = data.getData();
               //     if (f.exists()) f.delete();

                    break;

            }





    }   

     private 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.putExtra("outputX", 200);
                intent.putExtra("outputY", 200);
                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_FROM_CAMERA);
                } 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();


                }
            }
        }   

【问题讨论】:

标签: android save crop


【解决方案1】:

最佳做法是保存裁剪的位图并获取您保存的文件名。

【讨论】:

  • 谢谢。我该怎么做?
猜你喜欢
  • 1970-01-01
  • 2017-02-05
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
  • 2021-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多