I am using multiple buttons with OnClickListener() that when clicked startActivityForResult(). In the OnActivityResult() method, there are different actions to be performed. How do I get the right button to go the the right result?
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.camImgButton:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, picture);
break;
case R.id.galImgButton:
i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(i, REQUEST_CODE);
break;
case R.id.txtButton:
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch ( ) {
case :
if (resultCode == RESULT_OK) {
// We need to recyle unused bitmaps
if (bmp != null) {
bmp.recycle();
}
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
display.setImageBitmap(bmp);
break;
case :
InputStream stream = null;
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
// We need to recyle unused bitmaps
if (bmp != null) {
bmp.recycle();
}
stream = getContentResolver().openInputStream(data.getData());
bmp = BitmapFactory.decodeStream(stream);
display.setImageBitmap(bmp);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (stream != null)
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
you can start activity for result with different request codes for each button, and in your OnActivityResult method check for the requestCode sent back and match it with the action you want, which I think you already have in your code
Related
I used below codes for taking image from camera and put it in Image View (imgViewAds).
private void BtnPhoto_Click(object sender, EventArgs e)
{
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.SetVmPolicy(builder.Build());
Intent cameraIntent = new Intent(Android.Provider.MediaStore.ActionImageCapture);
var activities = PackageManager.QueryIntentActivities(cameraIntent, 0);
if (activities.Count > 0)
{
addAds.ImageName = Guid.NewGuid().ToString() + ".jpg";
Java.IO.File imageFile = new Java.IO.File(AdsAdapter.ImagePath(addAds.ImageName));
Android.Net.Uri imageUri = Android.Net.Uri.FromFile(imageFile);
cameraIntent.PutExtra(MediaStore.ExtraSizeLimit, 1024*10);
cameraIntent.PutExtra(MediaStore.ExtraOutput, imageUri);
StartActivityForResult(cameraIntent, 0);
}
else
{
Toast.MakeText(this, "Not Camera", ToastLength.Long).Show();
}
}
And here is OnActivityResult the the camare send result here.
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (resultCode == Result.Ok && (data != null))
{
Bundle extras = data.Extras;
Bitmap imageBitmap = (Bitmap)extras.Get("data");
imgViewAds.SetImageBitmap(imageBitmap);
MemoryStream stream = new MemoryStream();
imageBitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
addAds.AdsImage = stream.ToArray();
}
base.OnActivityResult(requestCode, resultCode, data);
}
but the data that sending to OnActivityResult is null and Image did not come to Image View.
When you pass EXTRA_OUTPUT with a URI to write to, the camera intent will be null and the picture is in the URI that you passed in.
so you could simply remove these two lines:
Android.Net.Uri imageUri = Android.Net.Uri.FromFile(imageFile);
cameraIntent.PutExtra(MediaStore.ExtraOutput, imageUri);
note that in this way you get the thumbnail of the image. so if you want the whole image you could use something like this (I haven't tested the code, but you could get the idea):
if (requestCode == CAMERA_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
addAds.ImageName = Guid.NewGuid().ToString() + ".jpg";
Java.IO.File imageFile = new Java.IO.File(AdsAdapter.ImagePath(addAds.ImageName));
Uri uri = Uri.fromFile(file);
Bitmap bitmap;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
bitmap = cropAndScale(bitmap, 300); // if you mind scaling
profileImageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
hey. i used that code for pick image from storage. but if i press the back button in my gadget. he is fc . how to give condition on canceled pick from storage..and not force close?
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST) {
filePath = data.getData();
if(filePath != null) {
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}else if(filePath == null){
startActivity(new Intent(this,HalamanUser.class));
}
} else if (requestCode == CAMERA_REQUEST) {
Log.i("hello", "REQUEST cALL");
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
Log.i("hello", "Exception" + e.getMessage());
}
}else {
startActivity(new Intent(this,HalamanUser.class));
}
}
When the user presses back, the result is RESULT_CANCELED, and the data that you receive is null. So the app crashes when you call data.getData(), as you are calling getData() on a null object. The are a few ways to get around this: you can check what the resultCode is, and make sure it's RESULT_OK. You can also simply check whether the data Intent is null before trying to get the data from it:
if (requestCode == PICK_IMAGE_REQUEST) {
if (data != null) {
filePath = data.getData();
} else {
// Note: if filePath is by default null, you don't need this else statement
filePath = null;
}
if (filePath != null) {
...
I have an activity where click of button camera activity is launched . Sometimes onActivityResult is called and sometimes not . Even after restarting the device the onActivityResult is not called nor does it restart the current activity . Any solution to this weird behavior ?
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
String imageUri = data.toURI();
Uri uri = Uri.parse(imageUri);
try {
mBitmap = Media.getBitmap(getContentResolver(), uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// if result is ok returns the bitmap
// mBitmap = (Bitmap) data.getExtras().get("data");
mImageView.setImageBitmap(mBitmap);
new Thread(postTheImage).start();
} else {
Toast.makeText(getApplicationContext(), "Error during capturing the image", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.capture_image_button) {
// Open the camera to capture the image
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
You are not providing the camera activity with the information it requires. You need to supply it a file URI.
Please refer to the following
onActivityResult returned from a camera, Intent null
hi i am pretty new to android programming, this might be a simple problem for some but i am quite new to this. i want to open the gallery from one intent but want the picked image to be displayed on another intent i used the following code
public class GalActivity extends Activity {
Context ctx;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.result);
}
private static final int REQUEST_CODE = 1;
protected Bitmap bitmap;
private ImageView imageView=null;
/** Called when the activity is first created. */
// #Override
public void pickImage(View View) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
}
// ctx=this;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
// We need to recyle unused bitmaps
if (bitmap != null) {
bitmap.recycle();
}
InputStream stream = getContentResolver().openInputStream(
data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
the code works fine if the image is to be displayed on the same intent. but what should i do if it has to be displayed on another intent say "activity2".
As Bitmap implements Parcelable Interface, you can send it through intent directly, so do following in your onActivityResult Code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
// We need to recyle unused bitmaps
if (bitmap != null) {
bitmap.recycle();
}
InputStream stream = getContentResolver().openInputStream(
data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(bitmap);
Intent intent= new Intent(this, SecondActivity.class);
intent.putExtra("BitmapImage", bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
and Read this Bitmap object in Another Activity by:
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
I want to upload image from my phone gallery into my application .In my application there is button named upload. when i click button,it should move to gallery and in gallery if i select image that selected image should display as thumbnail in application.I want to upload 10 images, from gallery in my application.
On click of the gallery button, start startActivityForResult as follows:
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY);
Consequently, detect GET_FROM_GALLERY (which is a static int, any request number of your choice e.g., public static final int GET_FROM_GALLERY = 3;) inside onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Detects request codes
if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
To view gallery:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),REQUEST_CODE);
and to use it in your app:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch (requestCode) {
case REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
//data gives you the image uri. Try to convert that to bitmap
break;
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.e(TAG, "Selecting picture cancelled");
}
break;
}
} catch (Exception e) {
Log.e(TAG, "Exception in onActivityResult : " + e.getMessage());
}
}
This is the way to go:
startActivityForResult(
new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI
),
GET_FROM_GALLERY
);