调用Android相机裁剪图片太大不能返回的问题解决

http://www.linuxidc.com/Linux/2012-11/73940.htm
这个地址吧问题讲解的很详细~
解决方案也正确~我就是按照那个改的~
核心代码在下方~方便遇到一样问题的同学解决

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
public class MainActivity extends Activity {

    // data
    private static final String TAG = "MainActivity";
    private static final int TAKE_BIG_PICTURE = 1;
    private static final int TAKE_SMALL_PICTURE = 2;
    private static final int CROP_BIG_PICTURE = 3;
    private static final int CROP_SMALL_PICTURE = 4;
    private static final int CHOOSE_BIG_PICTURE = 5;
    private static final int CHOOSE_SMALL_PICTURE = 6;
    private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";
    private Uri imageUri;// to store the big bitmap

    // views
    private ImageView imageView;

    private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 2);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);
        intent.putExtra("scale", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection
        startActivityForResult(intent, requestCode);
    }

    private Bitmap decodeUriAsBitmap(Uri uri) {
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(getContentResolver()
                    .openInputStream(uri));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return bitmap;
    }

    /** Handle click events */
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
        case R.id.buttonTakeBigPicture:
            if (imageUri == null)
                Log.e(TAG, "image uri can't be null");
            // capture a big bitmap and store it in Uri
            intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// action is
                                                                    // capture
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, TAKE_BIG_PICTURE);
            break;
        case R.id.buttonTakeSmallPicture:
            intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// action is
                                                                    // capture
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, TAKE_SMALL_PICTURE);
            break;
        case R.id.buttonChooseBigPicture:
            intent = new Intent(Intent.ACTION_GET_CONTENT, null);
            intent.setType("image/*");
            intent.putExtra("crop", "true");
            intent.putExtra("aspectX", 2);
            intent.putExtra("aspectY", 1);
            intent.putExtra("outputX", 600);
            intent.putExtra("outputY", 300);
            intent.putExtra("scale", true);
            intent.putExtra("return-data", false);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            intent.putExtra("outputFormat",
                    Bitmap.CompressFormat.JPEG.toString());
            intent.putExtra("noFaceDetection", false); // no face detection
            startActivityForResult(intent, CHOOSE_BIG_PICTURE);
            break;
        case R.id.buttonChooseSmallPicture:
            intent = new Intent(Intent.ACTION_GET_CONTENT, null);
            intent.setType("image/*");
            intent.putExtra("crop", "true");
            intent.putExtra("aspectX", 2);
            intent.putExtra("aspectY", 1);
            intent.putExtra("outputX", 200);
            intent.putExtra("outputY", 100);
            intent.putExtra("scale", true);
            intent.putExtra("return-data", true);
            intent.putExtra("outputFormat",
                    Bitmap.CompressFormat.JPEG.toString());
            intent.putExtra("noFaceDetection", true); // no face detection
            startActivityForResult(intent, CHOOSE_SMALL_PICTURE);
            break;
        default:
            break;
        }
    }

    /** Activity life cycle */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // views
        imageView = (ImageView) findViewById(R.id.imageView);

        // instantiate
        imageUri = Uri.parse(IMAGE_FILE_LOCATION);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {// result is not correct
            Log.e(TAG, "requestCode = " + requestCode);
            Log.e(TAG, "resultCode = " + resultCode);
            Log.e(TAG, "data = " + data);
            return;
        } else {
            switch (requestCode) {
            case TAKE_BIG_PICTURE:
                Log.d(TAG, "TAKE_BIG_PICTURE: data = " + data);// it seems to be
                                                                // null
                // TODO sent to crop
                cropImageUri(imageUri, 780, 600, CROP_BIG_PICTURE);
                // or decode as bitmap and display it
                // if(imageUri != null){
                // Bitmap bitmap = decodeUriAsBitmap(imageUri);
                // imageView.setImageBitmap(bitmap);
                // }
                break;
            case CROP_BIG_PICTURE:// from crop_big_picture
                Log.d(TAG, "CROP_BIG_PICTURE: data = " + data);// it seems to be
                                                                // null
                if (imageUri != null) {
                    Bitmap bitmap = decodeUriAsBitmap(imageUri);
                    imageView.setImageBitmap(bitmap);
                }
                break;
            case TAKE_SMALL_PICTURE:
                Log.i(TAG, "TAKE_SMALL_PICTURE: data = " + data);
                // TODO sent to crop
                cropImageUri(imageUri, 260, 200, CROP_SMALL_PICTURE);
                // or decode as bitmap and display it
                // if(imageUri != null){
                // Bitmap bitmap = decodeUriAsBitmap(imageUri);
                // imageView.setImageBitmap(bitmap);
                // }
                break;
            case CROP_SMALL_PICTURE:
                if (imageUri != null) {
                    Bitmap bitmap = decodeUriAsBitmap(imageUri);
                    imageView.setImageBitmap(bitmap);
                } else {
                    Log.e(TAG, "CROP_SMALL_PICTURE: data = " + data);
                }
                break;
            case CHOOSE_BIG_PICTURE:
                Log.d(TAG, "CHOOSE_BIG_PICTURE: data = " + data);// it seems to
                                                                    // be null
                if (imageUri != null) {
                    Bitmap bitmap = decodeUriAsBitmap(imageUri);
                    imageView.setImageBitmap(bitmap);
                }
                break;
            case CHOOSE_SMALL_PICTURE:
                if (data != null) {
                    Bitmap bitmap = data.getParcelableExtra("data");
                    imageView.setImageBitmap(bitmap);
                } else {
                    Log.e(TAG, "CHOOSE_SMALL_PICTURE: data = " + data);
                }
                break;
            default:
                break;
            }
        }
    }
}

使用Uri来传递图片~就可以避免文件过大的问题~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值