本篇文章主要介绍了"获取相册图片 照相图片 并剪裁 完美适配60版本的相册错误",主要涉及到方面的内容,对于Android开发感兴趣的同学可以参考一下:
本次解决方案使用了http://download.csdn.net/user/syif88 和 https://github.com/jdamcd/androi...
本次解决方案使用了http://download.csdn.net/user/syif88 和 https://github.com/jdamcd/android-crop两个demo,主要还是裁剪图片的方案,
主要解决的问题为android原生的裁剪图片在6.0后的系统可能引发的相册错误问题,我不知道怎么解决原生模块,只好用折中的方法,三方裁剪,如果你有更好的方法,欢迎交流~~
我在网上找了很多调用原生裁剪的demo,其中不乏深度处理过的,但是在nexus6 android 6.0系统后运行代码都会报相册出错错误,所以才有我下面不得不做的事,好了,看代码吧~
第一步,导入compile ‘com.soundcloud.android:android-crop:1.0.1@aar’;
第二步,添加Manifest
第三步,打开相册使用代码Crop.pickImage(activity);
第四步,直接打开裁剪使用Crop.of(inputUri, outputUri).asSquare().start(activity);
第五步,回调方法onActivityResult()中,
switch (requestCode) {
case Configs.SystemPicture.PHOTO_REQUEST_TAKEPHOTO: // 拍照
beginCrop(photoUri);
break;
case Configs.SystemPicture.PHOTO_REQUEST_CUT: //接收处理返回的图片结果,这个过程比较重要if (photoUri == null)
break;
/*Bitmap bit = data.getExtras().getParcelable("data"); //不要再用data的方式了,会出现activity result 的时候data == null的空的情况
iv_show.setImageBitmap(bit);*/try {
cropBitmap = getBitmapFromUri(photoUri, this); //通过获取uri的方式,直接解决了报空和图片像素高的oom问题if (cropBitmap != null) {
riv_avatar.setImageBitmap(cropBitmap);
}
} catch (Exception e) {
e.printStackTrace();
}
//下面可以用来上传pc服务端
File file = FileTools.getFileByUri(this, photoUri);
Log.d("File", file.toString());
break;
}
if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) {
beginCrop(data.getData());
} elseif (requestCode == Crop.REQUEST_CROP) {
handleCrop(resultCode, data);
}
其他调用到的 public Bitmap getBitmapFromUri(Uri uri,Context mContext)
{
try
{
// 读取uri所在的图片
Bitmap bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), uri);
return bitmap;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
private void beginCrop(Uri source) {
Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
Crop.of(source, destination).asSquare().start(this);
}
private void handleCrop(int resultCode, Intent result) {
if (resultCode == RESULT_OK) {
riv_avatar.setImageDrawable(null);
riv_avatar.setImageURI(Crop.getOutput(result));
} else if (resultCode == Crop.RESULT_ERROR) {
Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
}
}
不喜欢ios风格的这个弹窗,直接换成原生的,调用v7的dialog也还是好看,替换方法名openDialog()为下面的代码
/*****
* 打开选择框
* @param context Context Activity上下文对象
* @param uri Uri
*/publicstaticvoidopenDialog(final Activity context, final Uri uri){
new AlertDialog.Builder(context)
.setTitle("设置头像")
.setNegativeButton("相册", new DialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int which) {
dialog.dismiss();
Crop.pickImage(context);
}
})
.setPositiveButton("拍照", new DialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
startCamearPicCut(context,uri);
}
}).show();
}
本文demo地址http://download.csdn.net/detail/you943047219/9511387
').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('
').text(i));
};
$numbering.fadeIn(1700);
});
});
以上就介绍了获取相册图片 照相图片 并剪裁 完美适配60版本的相册错误,包括了方面的内容,希望对Android开发有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_925935.html