Android编程图片操作类定义与用法示例【拍照,相册选图及裁剪】

 更新时间:2018年02月09日 10:06:35   作者:迟做总比不做强  
这篇文章主要介绍了Android编程图片操作类定义与用法,涉及Android拍照,相册选图及裁剪等图片操作功能及权限控制相关操作技巧,需要的朋友可以参考下

本文实例讲述了Android编程图片操作类定义与用法。分享给大家供大家参考,具体如下:

主界面类:拍照及选择相册图片

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
/**
 * Android中图片操作(拍照,相册图片选择及图片裁剪)
 * 作者:ldm
 * 时间:20162016/7/11 09:09
 */
public class ImageTestActivity extends Activity implements View.OnClickListener {
  //拍照
  private Button take_photo;
  //从相册中选择图片
  private Button local_pic;
  //图片展示
  private ImageView upload_image;
  //定义操作常量
  private final static int TAKE_PHOTO_REQUEST = 1;
  private final static int LOCAL_PICS_REQUEST = 2;
  private final static int UPLOAD_PIC_REQUEST = 3;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_test);
    //初始化控件及监听事件
    initViews();
  }
  private void initViews() {
    this.upload_image = (ImageView) findViewById(R.id.upload_image);
    this.take_photo = (Button) findViewById(R.id.take_photo);
    this.local_pic = (Button) findViewById(R.id.local_pics);
    this.take_photo.setOnClickListener(this);
    this.local_pic.setOnClickListener(this);
  }
  @Override
  public void onClick(View view) {
    if (view.getId() == R.id.take_photo) {//拍照
      //调用系统拍照In
      Intent photoIn = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      startActivityForResult(photoIn, TAKE_PHOTO_REQUEST);
    } else if (view.getId() == R.id.local_pics) {//从相册选择
      Intent picsIn = new Intent(Intent.ACTION_GET_CONTENT);
      picsIn.setType("image/*");//设置选择的数据类型为图片类型
      startActivityForResult(picsIn, LOCAL_PICS_REQUEST);
    }
  }
  //拍照或选择相册后,数据在这里处理
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (null == data) {
      return;
    }
    switch (requestCode) {
      case TAKE_PHOTO_REQUEST:
        Bundle bundle = data.getExtras();//获取到图片数据
        if (null != bundle) {
          Bitmap bm = bundle.getParcelable("data");
          //把图片展示在ImView上
//          upload_image.setImageBitmap(bm);
          //对图片剪
          Uri uri = ImageUtils.saveBitmapToSdCard(bm);
          startImageCrop(uri);
        }
        break;
      case LOCAL_PICS_REQUEST:
        Uri uri = data.getData();//从图片的Uri是以cotent://格式开头的
        //获取到图片
        Bitmap bm = ImageUtils.uri2Bitmap(ImageTestActivity.this, uri);
        //把图片展示在ImView上
//        upload_image.setImageBitmap(bm);
        //把拍照的图片保存到本地并转换成文件格式的Uri
        Uri fileUri = ImageUtils.saveBitmapToSdCard(bm);
        //对图片剪
        startImageCrop(fileUri);
        break;
      case UPLOAD_PIC_REQUEST:
        //把裁剪后的图片展示出来
        Bundle b = data.getExtras();
        Bitmap bitmap = b.getParcelable("data");
        //图片展示出来
        upload_image.setImageBitmap(bitmap);
        break;
    }
  }
  /**
   * @param
   * @description 图片裁剪
   * @author ldm
   * @time 2016/7/11 10:07
   */
  private void startImageCrop(Uri uri) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");//设置Uri及类型
    intent.putExtra("crop", "true");//
    intent.putExtra("aspectX", 2);//X方向上的比例
    intent.putExtra("aspectY", 1);//Y方向上的比例
    intent.putExtra("outputX", 200);//裁剪区的X方向宽
    intent.putExtra("outputY", 100);//裁剪区的Y方向宽
    intent.putExtra("scale", true);//是否保留比例
    intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
    intent.putExtra("return-data", true);//是否将数据保留在Bitmap中返回dataParcelable相应的Bitmap数据
    startActivityForResult(intent, UPLOAD_PIC_REQUEST);
  }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <Button
    android:id="@+id/take_photo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="拍照上传" />
  <Button
    android:id="@+id/local_pics"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="本地图库上传" />
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="图片信息展示"
    android:layout_marginLeft="10dp"
    android:textSize="16sp"/>
  <ImageView
    android:id="@+id/upload_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"/>
</LinearLayout>

图片操作工具类

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.text.format.DateFormat;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Locale;
/**
 * description:从图片中获取到的Uri是以content://开头的,从U中找到对应图片
 * 作者:ldm
 * 间:20162016/7/11 09:47
 */
public class ImageUtils {
  public static Bitmap uri2Bitmap(Context mContext, Uri uri) {
    InputStream in = null;
    try {
      in = mContext.getContentResolver().openInputStream(uri);
      //从输入流中获取到图片
      Bitmap bm = BitmapFactory.decodeStream(in);
      in.close();
      return bm;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
  /**
   * @param
   * @description 保存图片到手机SD卡, 并返回图片对应的文件i
   * @author ldm
   * @time 2016/7/11 9:55
   */
  public static Uri saveBitmapToSdCard(Bitmap bm) {
    //自定义图片名称
    String name = DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".png";
    //定义图片存放的位置
    File tempFile = new File("/sdcard/Image/");
    if (!tempFile.exists()) {
      tempFile.mkdir();
    }
    String fileName = "/sdcard/Image/" + name;
    File pic = new File(fileName);
    try {
      FileOutputStream os = new FileOutputStream(pic);
      //对图片进行压缩
      bm.compress(Bitmap.CompressFormat.PNG, 100, os);
      os.flush();
      os.close();
      return Uri.fromFile(pic);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
}

最后不要忘记在AndroidManifest.xml中添加 相应权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

注:更多关于Android权限控制的说明可点击此处查看Android权限操作说明

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android自定义Drawable之在Drawable中部指定透明区域方法示例

    Android自定义Drawable之在Drawable中部指定透明区域方法示例

    对于不同的屏幕密度、不同的设备方向,不同的语言和区域,都会涉及到备选 drawable 资源,下面这篇文章主要给你大家介绍了关于Android自定义Drawable之在Drawable中部指定透明区域的相关资料,需要的朋友可以参考下
    2018-07-07
  • Android xUtils更新到3.0后的基本使用规则详解

    Android xUtils更新到3.0后的基本使用规则详解

    xUtils是基于android的开发框架,简化了很多的开发步骤,可以说是非常好的开发工具。下面小编给大家带来了Android xUtils更新到3.0后的基本使用规则详解,感兴趣的朋友一起学习吧
    2016-08-08
  • android 引导界面的实现方法

    android 引导界面的实现方法

    现在越来越多程序都有引导页面了。网上资料不全。现在自己实现下。
    2013-06-06
  • android 退出程序解决内存释放的问题

    android 退出程序解决内存释放的问题

    做Android项目的时候发现一个问题:当应用程序退出了,点击"设置"查看应用程序,界面显示着可以点击"强制关闭 由于这个问题我发现了一个更加严重的问题,那就是,在我应用程序退出之后,系统并没有释放掉我应用程序所占内存
    2012-11-11
  • Android实现滚动刻度尺效果

    Android实现滚动刻度尺效果

    本篇文章主要介绍了Android实现滚动刻度尺效果,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • Android布局技巧之创建可重用的UI组件

    Android布局技巧之创建可重用的UI组件

    这篇文章主要为大家详细介绍了Android布局技巧之创建可重用的UI组件,文中提到了include标签的使用方法,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • Android实现生成二维码并保存到相册

    Android实现生成二维码并保存到相册

    这篇文章主要介绍了如何利用Android实现二维码的生成,并且保存到本地相册。文中的示例代码讲解详细,感兴趣的小伙伴快跟随小编学习一下
    2022-04-04
  • Android 使用 RxJava2 实现倒计时功能的示例代码

    Android 使用 RxJava2 实现倒计时功能的示例代码

    本篇文章主要介绍了Android 使用 RxJava2 实现倒计时功能的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • 详解Android ViewCompat的作用

    详解Android ViewCompat的作用

    这篇文章主要介绍了详解Android ViewCompat的作用的相关资料,需要的朋友可以参考下
    2017-07-07
  • Android自定义控件之水平圆点加载进度条

    Android自定义控件之水平圆点加载进度条

    这篇文章主要为大家详细介绍了Android自定义控件之水平圆点加载进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06

最新评论