I am trying to use camera class in my app. I just want to click a picture and set on the imageview and then posting it on some url. Posting on url working fine but sometime problem occurs while clicking any picture and resuming back to same activity from where I am navigating to camera app. It works fine on HTC wildfire (2.2 version) but sometime gives exception (failure chance 1/25) but when i test it on Sony xperia miro or samsung tab (4.0 version) it gives same exception many times (failure chance 20/25). I am not getting where the problem exists because sometimes app runs smoothly without any exception but with 4.0 or above version it force closes many times but sometime works fine on it.
Exception is : java.lang.RuntimeException: Unable to resume activity {fable.eventippo/fable.eventippo.EventsIppoPaymentActivity}:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=tabHome, request=1, result=-1, data=Intent
{ dat=content://media/external/images/media/17271 }} to activity {fable.eventippo/fable.eventippo.EventsIppoPaymentActivity}:
java.lang.ClassCastException: fable.eventippo.Home cannot be cast to fable.eventippo.AddEvent
Complete code is given here.
Button Onclick.
browse = (Button) findViewById(R.id.browse);
browse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final CharSequence[] items = { "Camera", "Gallery" };
AlertDialog.Builder builder = new AlertDialog.Builder(
getParent());
builder.setTitle("Browse From");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[item] == "Camera") {
PackageManager pm = getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT,
MyFileContentProviders.CONTENT_URI);
getParent().startActivityForResult(i,
CAMERA_REQUEST);
} else {
Toast.makeText(getParent(),
"Camera is not available",
Toast.LENGTH_LONG).show();
}
} else if (items[item] == "Gallery") {
try {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
getParent().startActivityForResult(
Intent.createChooser(intent,
"Select Picture"), PICK_IMAGE);
} catch (Exception e) {
Toast.makeText(getParent(), e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
On Activity Result:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bundle bn = data.getExtras();
switch (requestCode) {
case PICK_IMAGE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImageUri = data.getData();
String filepath = null;
try {
// OI FILE Manager
String filemanagerstring = selectedImageUri.getPath();
// MEDIA GALLERY
String selectedImagePath = getPath(selectedImageUri);
// logo.setImageURI(selectedImageUri);
if (selectedImagePath != null) {
filepath = selectedImagePath;
} else if (filemanagerstring != null) {
filepath = filemanagerstring;
} else {
Toast.makeText(getApplicationContext(), "Unknown path",
Toast.LENGTH_LONG).show();
Log.e("Bitmap", "Unknown path");
}
if (filepath != null) {
// /upload.setText(filepath);
decodeFile(filepath);
} else {
// filePath = null;
bitmap = null;
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Internal error",
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
break;
case CAMERA_REQUEST:
if (resultCode == Activity.RESULT_OK) {
File out = new File(getFilesDir(), "newImage.jpg");
if (!out.exists()) {
Toast.makeText(getBaseContext(),
"Error while capturing image", Toast.LENGTH_LONG)
.show();
return;
}
String filepath = out.getAbsolutePath().toString();
decodeFile(filepath);
}
break;
default:
}
Complete Exception is shown on following image
If you need anything more please tell me.
Thanks in Advance
Waiting for answer