我想从相册中选择照片并将其设置在我的ImageView中,同时我也想从相机中拍摄照片并将其设置在同一个ImageView中。
我对此非常困扰。
有人能帮我吗?
谢谢。
您可以通过以下方式处理相机视图的点击:
cameraImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
}
});
当你从拍摄照片的界面返回到当前 Activity 时,执行以下操作:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0)
{
if(data != null)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
photo = Bitmap.createScaledBitmap(photo, 80, 80, false);
imageView.setImageBitmap(photo);
}
else{
}
}
}
private String getRealPathFromURI(Uri contentUri) {
int columnIndex = 0;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
try {
columnIndex = cursor.getColumnIndexOrThrow
(MediaStore.Images.Media.DATA);
} catch (Exception e) {
Toast.makeText(ImageEditor.this, "Exception in getRealPathFromURI",
Toast.LENGTH_SHORT).show();
finish();
return null;
}
cursor.moveToFirst();
return cursor.getString(columnIndex);
}
您需要实现以下代码来从相机或图库中获取图片:
Take this variable :
AlertDialog dialog;
private static final int IMAGE_PICK = 1;
private static final int IMAGE_CAPTURE = 2;
private Bitmap profile_imageBitmap;
当按钮被点击时:
if (v == btn_uploadPhoto) {
dialog.show();
}
final String[] items = new String[] { "Take from camera",
"Select from gallery" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.select_dialog_item, items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Image");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
path = "";
Intent intent = new Intent(
"android.media.action.IMAGE_CAPTURE");
File folder = new File(Environment
.getExternalStorageDirectory() + "/LoadImg");
if (!folder.exists()) {
folder.mkdir();
}
final Calendar c = Calendar.getInstance();
String new_Date = c.get(Calendar.DAY_OF_MONTH) + "-"
+ ((c.get(Calendar.MONTH)) + 1) + "-"
+ c.get(Calendar.YEAR) + " " + c.get(Calendar.HOUR)
+ "-" + c.get(Calendar.MINUTE) + "-"
+ c.get(Calendar.SECOND);
path = String.format(
Environment.getExternalStorageDirectory()
+ "/LoadImg/%s.png", "LoadImg(" + new_Date
+ ")");
File photo = new File(path);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
startActivityForResult(intent, 2);
} else { // pick from file
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Choose a Photo"),
IMAGE_PICK);
}
}
});
dialog = builder.create();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == IMAGE_PICK
|| requestCode == IMAGE_CAPTURE) {
switch (requestCode) {
case IMAGE_PICK:
this.imageFromGallery(resultCode, data);
img_myProfile.setImageBitmap(null);
img_myProfile.setImageBitmap(setphoto);
break;
case IMAGE_CAPTURE:
this.imageFromGallery(resultCode, data);
img_myProfile.setImageBitmap(null);
img_myProfile.setImageBitmap(setphoto);
break;
default:
break;
}
}
}
private void imageFromGallery(int resultCode, Intent data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
profile_Path = cursor.getString(columnIndex);
cursor.close();
setphoto = BitmapFactory.decodeFile(profile_Path);
}
private void imageFromCamera(int resultCode, Intent data) {
updateImageView((Bitmap) data.getExtras().get("data"));
}
private void updateImageView(Bitmap newImage) {
setphoto = newImage.copy(Bitmap.Config.ARGB_8888, true);
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}