您的位置:首页 > 移动开发 > Android开发

android选择图片、拍照、并剪裁

2016-03-18 15:11 489 查看
最近弄从相册中选择图片、拍照后裁剪。刚刚开始的时候使用5.0手机测试的,没什么问题,后来经过测试之后,发现在某些4.4的手机上选择图片会失败,后来修改了一下代码,基本上适配了当时的问题,这里记录一下解决问题的具体代码,首先我写了一个demo如下:

布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity">

<ImageView
android:id="@+id/img"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@mipmap/ic_launcher"/>

<Button
android:id="@+id/btnGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="从相册中选择照片"/>

<Button
android:id="@+id/btnCamera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="调用系统相机拍照"/>
</LinearLayout>


运行的效果如下:



布局中有一个ImageView用来显示剪裁后的图片,下面有两个Button分别是调用系统相册和调用系统相机。实现代码如下:

package com.todo.selectimage;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

private final static String TAG = MainActivity.class.getSimpleName();

ImageView imageView;
Button btnGallery;
Button btnCamera;

private static final int PHOTO_REQUEST_TAKE_PHOTO = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果

// 创建一个以当前时间为名称的文件
File tempFile = new File(Environment.getExternalStorageDirectory(),getPhotoFileName());

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView = (ImageView) findViewById(R.id.img);
btnGallery = (Button) findViewById(R.id.btnGallery);
btnCamera = (Button) findViewById(R.id.btnCamera);

// 相册中选取图片
btnGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
} else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
}
});

// 调用系统相机拍照
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 调用系统的拍照功能
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 指定调用相机拍照后照片的储存路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
startActivityForResult(intent, PHOTO_REQUEST_TAKE_PHOTO);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch (requestCode) {
case PHOTO_REQUEST_TAKE_PHOTO: // 拍照返回结果
startPhotoZoom(Uri.fromFile(tempFile), 150);
break;

case PHOTO_REQUEST_GALLERY: // 从相册中选取返回结果
if (data != null)
startPhotoZoom(data.getData(), 150);
break;

case PHOTO_REQUEST_CUT: // 裁剪之后返回结果
if (data != null)
setPicToView(data);
break;
}
super.onActivityResult(requestCode, resultCode, data);

}

/**
* 裁剪图片
* @param uri 要裁剪图片对应的uri
* @param size 参见的大小 (宽高)
*/
private void startPhotoZoom(Uri uri, int size) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");

// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);

// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", size);
intent.putExtra("outputY", size);
intent.putExtra("return-data", true);

startActivityForResult(intent, PHOTO_REQUEST_CUT);

}

/**
* 将进行剪裁后的图片显示到UI界面上
*/
private void setPicToView(Intent picdata) {
Bundle bundle = picdata.getExtras();
if (bundle != null) {
Bitmap photo = bundle.getParcelable("data");
imageView.setImageBitmap(photo);
}
}

// 使用系统当前日期加以调整作为照片的名称
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");
return dateFormat.format(date) + ".jpg";
}

}


上面的代码,有相关注释,这里不在解释了,需要注意的是我们用来启动相册、相机的Intent、Action等相关参数的选择上要注意,网上有很多类似的代码,但是可能某个参数的选择不当就可能造成在某些机器上异常,我们可以参考一下下面的博文来探索一下:

Android 4.4从图库选择图片,获取图片路径并裁剪

Intent的使用方式太多,这里就不分析具体的原因了,大家有兴趣的可以查看一下相关的文档,如果上述的代码在某些机型上出现问题希望大家留言通知一下,大家一起来解决这个问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息