码迷,mamicode.com
首页 > 移动开发 > 详细

android选择本地图片并裁剪

时间:2016-01-09 18:21:40      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

本文使用Android原生的选择和裁剪功能。下面是代码:

public class PickAndCropActivity extends AppCompatActivity implements View.OnClickListener
{
    public static final int REQUEST_CODE_PICK_IMG = 1;
    public static final int REQUEST_CODE_CROP_IMG = 2;
    /**
     * ImageView展示选择的图片和裁剪得到的图片
     */
    private ImageView img;
    /**
     * 选择得到的图片uri
     */
    private Uri mUri;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_and_crop);
        img = (ImageView) findViewById(R.id.img);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.pick:
                startActivityForResult(IntentUtils.generatePickImgIntent(), REQUEST_CODE_PICK_IMG);
                break;
            case R.id.crop:
                startActivityForResult(IntentUtils.generateCropImgIntent(mUri, 150), REQUEST_CODE_CROP_IMG);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        switch (requestCode)
        {
            case REQUEST_CODE_PICK_IMG:
                if (resultCode != RESULT_OK || data == null) break;
                mUri = data.getData();
                ContentResolver cr = this.getContentResolver();
                try
                {
                    Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(mUri));
                    img.setImageBitmap(bitmap);
                } catch (FileNotFoundException e)
                {
                    e.printStackTrace();
                }
                break;
            case REQUEST_CODE_CROP_IMG:
                if (resultCode != RESULT_OK || data == null) break;
                Bitmap bmap = data.getParcelableExtra("data");
                img.setImageBitmap(bmap);
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

 中间用到的IntentUtils类:

public class IntentUtils
{
    /**
     * 构造一个选择图片的Intent
     *
     * @return
     */
    public static Intent generatePickImgIntent()
    {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        return intent;
    }

    /**
     * 构造一个裁剪图片的Intent
     *
     * @param uri
     * @param size
     * @return
     */
    public static Intent generateCropImgIntent(Uri uri, int size)
    {
        Intent intent = new Intent("com.android.camera.action.CROP");
        // mUri是已经选择的图片Uri
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // 裁剪框比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // 输出图片大小
        intent.putExtra("outputX", size);
        intent.putExtra("outputY", size);
        intent.putExtra("return-data", true);
        return intent;
    }
}

 

android选择本地图片并裁剪

标签:

原文地址:http://www.cnblogs.com/hsji/p/5116797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!