源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android编程实现调用系统图库与裁剪图片功能

  • 时间:2020-03-10 14:52 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android编程实现调用系统图库与裁剪图片功能
本文实例讲述了Android编程实现调用系统图库与裁剪图片功能。分享给大家供大家参考,具体如下: 在Android开发中,调用系统图库和裁剪照片是很常见的需求。相对于自己实现这种功能,直接调用系统具有诸多优点,如不用考虑屏幕适配,不用担心性能问题,等等。因此,对于一般的需求,建议直接调用系统的功能,简便高效! 首先上效果图: [img]http://files.jb51.net/file_images/article/201701/201713091312236.jpg?201703092129[/img]  [img]http://files.jb51.net/file_images/article/201701/201713092133084.jpg?201703092219[/img]  [img]http://files.jb51.net/file_images/article/201701/201713092223320.jpg?201703092324[/img] 一、只调用系统图库(不裁剪),返回用户选择的图片。(只支持单选,如需多选则需要自己实现,可参考Android编程实现仿QQ照片选择器(按相册分类显示,多选添加)[b][url=http://xiazai.jb51.net/201701/yuanma/AppImagePicker(jb51.net).rar]源码[/url][/b]。) 1.跳转至系统图库页面:
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_IMAGE);

2.在onActivityResult中接收系统图库返回的信息(也就是用户选择的照片)。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != RESULT_OK || data == null) {
      return;
    }
    //select an image
    if (requestCode == SELECT_IMAGE) {
      //get image path from uri
      String imagePath = getImagePath(data.getData());
      return;
    }
}
private String getImagePath(Uri selectedImage) {
    String[] filePathColumn = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String imagePath = cursor.getString(columnIndex);
    cursor.close();
    System.out.println("image path:" + imagePath);
    return imagePath;
}

二、跳转至系统图库,选择照片,并裁剪。 1.跳转至系统图库:
//select image via system gallery, crop and save the new image file.
public void selectImageAndCrop(View view) {
    //After cropping, the image file will be stored here!
    cacheFile = imageCacheFolder + File.separator + "cache_" + System.currentTimeMillis() + ".jpg";
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    intent.putExtra("crop", "true");
    //width:height
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("output", Uri.fromFile(new File(cacheFile)));
    intent.putExtra("outputFormat", "JPEG");
    startActivityForResult(Intent.createChooser(intent, "Choose Image"), SELECT_IMAGE_CROP);
}

跳转之前需要传递的数据: (1)crop:传递一个true,告诉系统需要裁剪。 (2)aspectX和aspectY:裁剪框的宽高比。 (3)output:需要传递一个由文件路径cacheFile构建的uri,用户在图库页面选择照片之后会自动进入裁剪页面,裁剪之后图片会被保存在cacheFile这个位置。 裁剪完成之后,同样会回调onActivityResult方法(resultCode为RESULT_OK),并且图片会被保存在cacheFile这个位置,因此可以直接使用这个文件,例如将其设置为ImageView的资源。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != RESULT_OK) {
      return;
    }
    //select an image and crop
    if (requestCode == SELECT_IMAGE_CROP) {
       //compress the original image to save memory.
      BitmapFactory.Options opt = new BitmapFactory.Options();
      opt.inSampleSize = 4;
      imageView.setImageBitmap(BitmapFactory.decodeFile(cacheFile, opt));
    }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/214.htm]Android图形与图像处理技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/410.htm]Android开发入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/508.htm]Android调试技巧与常见问题解决方法汇总[/url]》、《[url=http://www.1sucai.cn/Special/381.htm]Android基本组件用法总结[/url]》、《[url=http://www.1sucai.cn/Special/375.htm]Android视图View技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/371.htm]Android布局layout技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/124.htm]Android控件用法总结[/url]》 希望本文所述对大家Android程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部