织梦CMS - 轻松建站从此开始!

我是农村人

当前位置: 主页 > 技术笔记 > Android >

Android设置头像兼容谷歌自带截图

时间:2015-09-09 15:48来源:未知 作者:admin 点击:
Android设置头像兼容谷歌自带截图
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
 
switch (requestCode) {
// 如果是直接从相册获取
case 1001 :
if (null != data && null != pThis) {
HealthUtil.savePhotoFromUriToUri(
HealthApp.getApplication(), data.getData(),
Uri.parse("file://" + pathin), false);
HealthUtil.startCrop(pThis, Uri.parse("file://" + pathin),
Uri.parse("file://" + pathout));
}
break;
// 如果是调用相机拍照时
case 1002 :
if (null != data && null != data.getData()) {
HealthUtil.startPhotoZoom(pThis, data.getData());
}
if (new File(pathin).exists()) {
HealthUtil.startCrop(pThis, Uri.parse("file://" + pathin),
Uri.parse("file://" + pathout));
}
break;
// 取得裁剪后的图片
case 1003 :
if (data != null) {
setPicToView(data, iv_set_photo);
}
new Thread(new Runnable() {
 
@Override
public void run() {
try {
(new File(pathin)).delete();
} catch (Exception e) {
// TODO: handle exception
}
}
}).start();
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
 
/**
* 保存裁剪之后的图片数据
* @param picdata
*/
public void setPicToView(Intent picdata, ImageView v) {
Bundle extras = picdata.getExtras();
if (extras != null && null != v) {
Bitmap photo = extras.getParcelable("data");
Uri fileUri = picdata.getData();
if (null != fileUri) {
String path = fileF + FILE_PHONTO_FILE + ".jpg";
restImg(path, v);
}
if (null != photo) {
BitmapDrawable drawable = new BitmapDrawable(getResources(),
photo);
CompressFormat format = Bitmap.CompressFormat.JPEG;
int quality = 100;
OutputStream stream = null;
try {
stream = new FileOutputStream(fileF + FILE_PHONTO_FILE
+ ".jpg");
photo.compress(format, quality, stream);
 
v.setBackground(drawable);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} finally {
if (null != stream)
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
restImg(pathout, v);
}
} else if (null != picdata.getData()) {
Uri dataurl = picdata.getData();
restImg(dataurl.getPath(), v);
} else {
restImg(pathout, v);
}
}
 
private void restImg(String path, ImageView v) {
if (new File(path).exists()) {
Bitmap bm = BitmapFactory.decodeFile(path);
BitmapDrawable drawable = new BitmapDrawable(getResources(), bm);
v.setBackground(drawable);
v.requestLayout();
}
}
 
/**
* 选择提示对话框
*/
private void ShowPickDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.set_photo)
.setItems(R.array.select_photo_dialog_items,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
if (which == 0) {
dialog.dismiss();
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
File mfile = new File(pathin);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(mfile));
startActivityForResult(intent, 1002);
 
} else if (which == 1) {
dialog.dismiss();
final Intent intent = new Intent(
Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
HealthUtil.addPhotoPickerExtras(intent,
Uri.parse("file://" + pathin));
startActivityForResult(intent, 1001);
}
}
}).show();
 
}

 

public static void startCrop(Activity mActivity, Uri uri, Uri outuri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
addPhotoPickerExtras(intent, outuri);
addCropExtras(intent, 207);
mActivity.startActivityForResult(intent, 1003);
}
 
public static void addPhotoPickerExtras(Intent intent, Uri photoUri) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setClipData(ClipData
.newRawUri(MediaStore.EXTRA_OUTPUT, photoUri));
}
public static void addCropExtras(Intent intent, int photoSize) {
intent.putExtra("crop", "true");
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", photoSize);
intent.putExtra("outputY", photoSize);
}
 
public static boolean savePhotoFromUriToUri(Context context, Uri inputUri,
Uri outputUri, boolean deleteAfterSave) {
FileOutputStream outputStream = null;
InputStream inputStream = null;
try {
outputStream = context.getContentResolver()
.openAssetFileDescriptor(outputUri, "rw")
.createOutputStream();
inputStream = context.getContentResolver()
.openInputStream(inputUri);
 
final byte[] buffer = new byte[16 * 1024];
int length;
int totalLength = 0;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
totalLength += length;
}
} catch (IOException e) {
 
return false;
} finally {
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (deleteAfterSave) {
context.getContentResolver().delete(inputUri, null, null);
}
}
return true;
}
 
/**
* 裁剪图片方法实现
* @param uri
*/
public static void startPhotoZoom(Activity mActivity, Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.putExtra("crop", "true");
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 207);
intent.putExtra("outputY", 207);
intent.putExtra("return-data", true);
mActivity.startActivityForResult(intent, 1003);
}
 
public static void startPhotoZoomX(Activity mActivity, Uri uri,
String macAddress) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.putExtra("crop", "true");
intent.putExtra("outputX", 207);
intent.putExtra("outputY", 207);
intent.putExtra("return-data", false);
 
String filePath = Environment.getExternalStorageDirectory()
+ FILE_SYMBOL1 + FILE_PHONTO + FILE_SYMBOL1 + FILE_PHONTO_FILE
+ ".jpg";
 
Uri ss = Uri.parse("file://" + filePath);
intent.putExtra(MediaStore.EXTRA_OUTPUT, ss);
mActivity.startActivityForResult(intent, 1003);
}
(责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------