/*
* 从相册获取
*/
public void gallery() {
// 激活系统图库,选择一张图片
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 2);
}
/*
* 从相机获取
*/
public void camera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 判断存储卡是否可以用,可用进行存储
if (hasSdcard()) {
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME)));
}
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 2://相册
if (data != null) {
// 得到图片的全路径
Uri uri = data.getData();
crop(uri);
}
break;
case 1://相机
if (hasSdcard()) {
tempFile = new File(Environment.getExternalStorageDirectory(),
PHOTO_FILE_NAME);
crop(Uri.fromFile(tempFile));
} else {
Toast.makeText(MyDataActivity.this, "未找到存储卡,无法存储照片!", 0).show();
}
break;
case 3://剪切
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
mydata_headImg.setImageBitmap(bitmap);
saveBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
/** 保存方法 */
public void saveBitmap(Bitmap bitmap) {
File file = new File(Environment.getExternalStorageDirectory()+ "/DYLC/");
if (!file.exists())
file.mkdir();
File tempFile = new File(file, PHOTO_FILE_NAME);
Log.e(TAG, "保存图片");
if (tempFile.exists()) {
tempFile.delete();
}
try {
FileOutputStream out = new FileOutputStream(tempFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ThreadRun(3, sp.getId(), tempFile.getPath());
}
private boolean hasSdcard() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}