import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import com.kidswant.ss.R;
import com.kidswant.ss.app.AppContext;
import java.io.File;
/**
* 从相册或拍照获取图片的工具类
*
* @author bruce.zhang
*
*/
public class SysPictureUtil
{
/**
* 相册
*/
public static final int REQUEST_ALBUM = 1;
/**
* 拍照
*/
public static final int REQUEST_CAMARE = 2;
/**
* 拍照还是相册获取图片
*/
private int requestFlag = REQUEST_ALBUM;
/**
* 上下文
*/
private Context context;
/**
* 缺省路径
*/
private String defaultFilePath = null;
/**
* 图片的uri
*/
private Uri mPicUri;
/**
* 单例
*/
static SysPictureUtil instance = null;
/**
* 同步锁
*/
private static Object obj = new Object();
private SysPictureUtil()
{
context = AppContext.getInstance();
// defaultFilePath = Constants.SDCARD_PATH + File.separator + Constants.ICON_PIC_DIR + "/" + FileUtils.getUniquePicName(".jpg", "pic");
String fileName = FileUtils.getUniquePicName(".jpg", "pic");
defaultFilePath = FileUtils.buildFilePath(FileUtils.ICON_PIC_DIR, fileName);
}
/**
* 获取实例
*
* @return
*/
public static SysPictureUtil getInstance()
{
synchronized (obj)
{
if (instance == null)
{
instance = new SysPictureUtil();
}
}
return instance;
}
/**
* 相机获取图片
*
* @param activity
* @param requestCode
* 请求code
*/
public Uri takePictureFromCamera(Activity activity, int requestCode)
{
// String filePath = Constants.SDCARD_PATH + File.separator + Constants.ICON_PIC_DIR + "/" + FileUtils.getUniquePicName(".jpg", "pic");
String fileName = FileUtils.getUniquePicName(".jpg", "pic");
String filePath = FileUtils.buildFilePath(FileUtils.ICON_PIC_DIR, fileName);
return takePictureFromCamera(activity, filePath, requestCode);
}
/**
* 相机获取图片
*
* @param activity
* @param filePath 文件路径
* @param requestCode 请求code
*
*/
public Uri takePictureFromCamera(Activity activity, String filePath, int requestCode)
{
if (!checkDir())
{
return null;
}
if (TextUtils.isEmpty(filePath))
{
filePath = defaultFilePath;
}
requestFlag = REQUEST_CAMARE;
try
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(filePath);
Uri uri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
activity.startActivityForResult(intent, requestCode);
mPicUri = uri;
}
catch (ActivityNotFoundException e)
{
WidgetUtil.ToastMessage(activity.getApplicationContext(), R.string.camera_invalid);
}
return mPicUri;
}
/**
* 图库获取图片
*
* @param activity
* @param requestCode
* 请求code
*/
public void takePictureFromAlbum(Activity activity, int requestCode)
{
if (!checkDir())
{
return;
}
requestFlag = REQUEST_ALBUM;
try
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
activity.startActivityForResult(intent, requestCode);
}
catch (ActivityNotFoundException e)
{
WidgetUtil.ToastMessage(activity.getApplicationContext(), R.string.album_invalid);
}
}
/**
* 裁剪图片(调用系统裁剪图片)
*/
public void cropImage(Activity activity, Uri uri, int outputX, int outputY, int requestCode)
{
if (uri == null) return;
this.mPicUri = uri;
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", outputX);
intent.putExtra("outputY", outputY);
intent.putExtra("return-data", true);
activity.startActivityForResult(intent, requestCode);
}
public void cropImage(Activity activity, Uri uri, Uri outputUri, int aspectX, int aspectY, int outputX, int outputY, int requestCode)
{
if (uri == null || outputUri == null) return;
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("scale", true);// 去黑边
intent.putExtra("scaleUpIfNeeded", true);// 去黑边
intent.putExtra("crop", "true");
intent.putExtra("aspectX", aspectX);
intent.putExtra("aspectY", aspectY);
intent.putExtra("outputX", outputX);
intent.putExtra("outputY", outputY);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
intent.putExtra("return-data", false);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // 是否去除面部检测, 如果你需要特定的比例去裁剪图片,那么这个一定要去掉,因为它会破坏掉特定的比例。
activity.startActivityForResult(intent, requestCode);
}
/**
* 获取拍照所得照片的绝对路径
*
* @return
*/
public String getImagePathFromCamera()
{
return getImagePath(mPicUri);
}
/**
* 查询图片的本地绝对路径
*
* @param uri
* @return
*/
public String getImagePath(Uri uri)
{
if (uri == null)
{
return "";
}
final String uriPath = uri.toString();
String path = "";
if ((uriPath).contains(Environment.getExternalStorageDirectory().getAbsolutePath()))
{
path = uriPath.substring(uriPath.indexOf(Environment.getExternalStorageDirectory().getAbsolutePath()));
}
else if ((uriPath).contains(Environment.getDataDirectory().getAbsolutePath()))
{
path = uriPath.substring(uriPath.indexOf(Environment.getDataDirectory().getAbsolutePath()));
}
else
{
Cursor cursor = null;
try
{
// cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
// if (cursor != null)
// {
// cursor.moveToFirst();
// path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
// }
path = UriFileUtils.getPath(context, uri);
}
finally
{
if (cursor != null)
{
cursor.close();
cursor = null;
}
}
}
return path;
}
public int getType()
{
return requestFlag;
}
/**
* 获取图片路径的URI
*
* @return
*/
public Uri getImageUri()
{
return mPicUri;
}
private boolean checkDir()
{
// if (FileUtil.hasSdcard()) {
// FileUtils.ensureAppPath(Constants.ICON_PIC_DIR);
FileUtils.getSaveFolder(FileUtils.ICON_PIC_DIR);
return true;
// } else {
// return false;
// }
}
}
调用拍照
Uri cameraPicUri = SysPictureUtil.getInstance().takePictureFromCamera(getActivity(), REQUEST_CAMERA);
裁剪
String fileName = FileUtils.(, );
String path = FileUtils.(FileUtils., fileName);
Uri outputUri = Uri.(+ path);
SysPictureUtil.().cropImage(getActivity(), uri, outputUri, , , outputX, outputY, requestCode);