I want to take photos from gallery and set it in my ImageView as 开发者_StackOverflow社区well as I want to take photo from camera and set it into my same Image View.
I am very much stuck on this.
Can anyone help me?
Thanks.
You can handle your camera view click this way:
cameraImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
}
});
Do this in your activity when you return after capturing image.
@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{
}
}
}
Get the open source code for gallery. Lookout onclick for the image selection. Launch your activity by setting the intent uri being genrated on th onclick .
In your application, get the intent data and get the real path from uri and then decode, set it as image view element .
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);
}
You must implement this code to take image from camera or gallery :
Take this variable :
AlertDialog dialog;
private static final int IMAGE_PICK = 1;
private static final int IMAGE_CAPTURE = 2;
private Bitmap profile_imageBitmap;
On Button click event :
if (v == btn_uploadPhoto) {
dialog.show();
}
Then in your activity's oncreate method :
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();
Now Out of Oncreate :
@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);
}
精彩评论