80,490
社区成员
发帖
与我相关
我的任务
分享protected void getImageFromAlbum() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");// 相片类型
startActivityForResult(intent, 0);
}
@TargetApi(Build.VERSION_CODES.FROYO)
protected void getImageFromCamera() {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date(System.currentTimeMillis());
String filename = format.format(date);
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File outputImage = new File(path, filename + ".jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == RESULT_OK && null != data) {
Uri uri = data.getData();
// pic = uri.toString();
// icon.setImageURI(uri);
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
p_icon.setImageBitmap(bitmap);
pic = uri.toString();
} catch (FileNotFoundException e) {
pic = null;
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (requestCode == 1 && resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
p_icon.setImageBitmap(bitmap);
pic = imageUri.toString();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
pic = null;
}
} else {
pic = null;
Toast.makeText(getApplicationContext(), "Unsuccessful!", Toast.LENGTH_LONG).show();
}
}