Reputation: 427
I am able to capture an image from gallery or camera and put it in imageview with following code:
1. Capture image from gallery or camera:
private void captureImageInitialization() {
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) { // pick from
// camera
if (item == 0) {
Intent intent = new Intent(
"android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, PICK_FROM_CAMERA);
} else {
// pick from file
/**
* To select an image from existing files, use
* Intent.createChooser to open image chooser. Android will
* automatically display a list of supported applications,
* such as image gallery or file manager.
*/
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_FILE);
}
}
});
dialog = builder.create();
}
2. Show in imageview:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case PICK_FROM_CAMERA:
mImageCaptureUri = data.getData();
imagepath = getPath(mImageCaptureUri);
bitmap = BitmapFactory.decodeFile(imagepath);
mImageView.setImageBitmap(bitmap);
// Log.d("camera ---- > ", "" + data.getExtras().get("data"));
new ProcessUploadAvatar2().execute();
break;
case PICK_FROM_FILE:
/**
* After selecting image from files, save the selected path
*/
mImageCaptureUri = data.getData();
imagepath = getPath(mImageCaptureUri);
bitmap2 = BitmapFactory.decodeFile(imagepath);
mImageView.setImageBitmap(bitmap2);
break;
}
}
Now my question is how to check if the image in the imageview has been taken by camera or gallery beyond the onActivityResult() method? I try to implement something like this:
public void NetAsync() {
if (mImageView.getDrawable() != null) {
if (mImageView = bitmap) {
new NetCheck().execute();
} else if (mImageView = bitmap2) {
new ProcessUploadAvatar2().execute();
}
}
}
Upvotes: 2
Views: 2175
Reputation: 161
You can easily get this via single check of request-codes in onActivityResult()... I mean those request-codes which you've sent while calling camera/gallery intent..
Upvotes: 1