在开发过程中遇到相机或者相册获取图片后截取图片时只有三星手机可以用其他手机全部没有效果。
原本使用的方法是:
public void scropPic(Uri uri) {
Intent intent = new Intent();
intent.setAction("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("return-data", true);
startActivityForResult(intent, 2);
}
据说是intent.putExtra("return-data", true); 不能用true。具体也不太了解。所以收集各位大神的方法后总结过来的。
接下来看详细代码。
先准备好这4个方法
1.
/**
* 进入相机拍照获取图片
* @param context
* @param REQUE_CODE_CAMERA
*/
private void getImageFromCamer(Context context,int REQUE_CODE_CAMERA) {
//判断sdcard是否存在
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
try {
//qwe为相片保存的文件夹名
File dir = new File(Environment.getExternalStorageDirectory() + "/" + "qwe");
if (!dir.exists())
dir.mkdirs();
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//asd为保存的相片名
File f = new File(dir, "asd");// localTempImgDir和localTempImageFileName是自己定义的名字
Uri u = Uri.fromFile(f);
intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
((Activity) context).startActivityForResult(intent, REQUE_CODE_CROP);
} catch (ActivityNotFoundException e) {
// TODO Auto-generated catch block
Toast.makeText(context, "没有找到储存目录", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(context, "没有储存卡", Toast.LENGTH_LONG).show();
}
}
2.
/**
* 进入相机获取图片
* @param context
* @param REQUE_CODE_PHOTO
*/
public static void getImageFromPhoto(Context context, int REQUE_CODE_PHOTO) {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
((Activity) context).startActivityForResult(intent, REQUE_CODE_PHOTO);
}
3.