I am having an issue with taking pictures on my Sony Xperia L phone (but it works for other phones like nexus and htc) when it runs as an android activity...
After taking the picture it directly returns back with a result code of RESULT_CANCELED.
following is my code for the application...
Toast.makeText(this, "Taking snapshot", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(intent.resolveActivity(getPackageManager()) != null){
Toast.makeText(this, " camera ready", Toast.LENGTH_SHORT).show();
startActivityForResult(intent, REQUEST_CAPTURE_PIC);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(this, "back from camera...", Toast.LENGTH_SHORT).show();
if (requestCode == REQUEST_CAPTURE_PIC || resultCode == RESULT_OK) {
Toast.makeText(this, "now saving..", Toast.LENGTH_SHORT).show();
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
img.setImageBitmap(imageBitmap);
}
}
I dont that this is a code issue. I think this has something to do with my phone camera application.
Thanks in advance.
Related
I have an intent that picks an image (I'm calling this in a fragment):
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/png");
intent.addCategory(Intent.CATEGORY_OPENABLE);
String[] mimetypes = {"image/png","image/jpg", "image/bmp", "image/gif"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
// special intent for Samsung file manager
Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
// if you want any file type, you can skip next line
sIntent.putExtra("CONTENT_TYPE", "image/png");
sIntent.addCategory(Intent.CATEGORY_DEFAULT);
Intent chooserIntent;
if (getContext().getPackageManager().resolveActivity(sIntent, 0) != null){
// it is device with Samsung file manager
chooserIntent = Intent.createChooser(sIntent, "Open file");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { intent});
} else {
chooserIntent = Intent.createChooser(intent, "Open file");
}
try {
startActivityForResult(chooserIntent, 548);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getContext(), "No suitable File Manager was found.", Toast.LENGTH_SHORT).show();
}
And in my activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult: request code: " + requestCode);
Log.d(TAG, "onActivityResult: result: " + resultCode + " Success = " + (resultCode == RESULT_OK));
if (requestCode == 548 && resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.d(TAG, "onActivityResult: URI: " + uri.toString());
Log.d(TAG, "onActivityResult: OK");
}
}
output:
onActivityResult: request code: 66084
onActivityResult: result: -1 Success = true
As you can see, the result was successful but the request code changes. so i can't check what was the request.
Your startActivityForResult() call should be on the same thing that your onActivityResult() is on.
So, if you want the onActivityResult() to be on the activity, call startActivityForResult() on the activity.
startActivityForResult() on the fragment intentionally changes the requestCode to minimize conflicts with other startActivityForResult() calls, particularly those made on other fragments in the same activity.
I am new to Java and Android but I have an app that should take a picture from the camera and save it as a file. I can start the camera and take a picture but in onActivityResult the resultCode is always RESULT_CANCELED (0). First I had an android.os.FileUriExposedException error but I followed this blog and the problem seems to be solved : https://medium.com/#ali.muzaffar/what-is-android-os-fileuriexposedexception-and-what-you-can-do-about-it-70b9eb17c6d0
Though I still have a resultCode with value 0 (RESULT_CANCEL).
Below is the code where I start the camera activity :
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
Uri uri = FileProvider.getUriForFile(
this,
this.getApplicationContext()
.getPackageName() + ".provider", file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
intent.addFlags(FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, uri, FLAG_GRANT_WRITE_URI_PERMISSION | FLAG_GRANT_READ_URI_PERMISSION);
}
}
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
And below is my onActivityResult (but resultCode is always 0) :
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Résultat de la capture de la photo
if (requestCode == CAMERA_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Finally I followed the exact instructions given here, https://developer.android.com/training/camera/photobasics , and it now works.
i'm writing an android app on java and need to let my users select and crop images from the gallery.
There is no problem when choosing an image from any native gallery, but when a user chooses to eater crop or choose an image from google photos app the app crushes.
I cannot figure out what is the source of the problem so any answer will be helpful
this is the code i'm using
class fields:
private Uri imageUri;
opening the camera:
private void camOpen() {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(Environment.getExternalStorageDirectory(), "file" + String.valueOf(System.currentTimeMillis()) + ".png");
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
imageUri = Uri.fromFile(f);
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
i.putExtra("return-data", true);
startActivityForResult(i, CAMERA_CODE);
}
opening the gallery:
private void galleryOpen() {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(i, "select file"), SELECT_PHOTO_CODE);
}
cropping the image:
private void cropImage() {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(imageUri, "image/*");
cropIntent.putExtra("crop", true);
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, CROP_CODE);
} catch (Exception e) {}
}
the result handler:
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_CODE && resultCode == Activity.RESULT_OK) {
cropImage();
} else if (requestCode == SELECT_PHOTO_CODE && resultCode == Activity.RESULT_OK) {
if (data != null) {
imageUri = data.getData();
cropImage();
}
} else if (requestCode == CROP_CODE && resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras();
Bitmap b = bundle.getParcelable("data");
hasImageChanged=true;
ivProfilePic.setImageBitmap(b);
capturedImage = b;
}
}
thank you for any useful help...
Android does not support crop intent because croping is not part of android api.
So i recommend you for Using library
the issue was with the crop intent. I ended up using the uCrop library and it fixed the problem.
so this was working lovely till yesterday when I upgraded my phone to 5.0.0 and tested the application out in a 5.0.0 emulator. Basically the purpose is to allow the user to take a photo, return it, open it to allow cropping then return it and set it as an imageview.
Everything worked fine before but now the intent in onActivityResult is null.
Here's code:
public void takeDisplayPicture(View view) {
final String TAKE_DISPLAY_PICTURE = "Take Display Picture";
Log.d(TAKE_DISPLAY_PICTURE, "Clicked");
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_CAPTURE);
} catch (ActivityNotFoundException e) {
String msg = "Your device doesn't support a camera!";
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toast.show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final String RETURN_DISPLAY_PICTURE = "Return Display Picture";
Log.d(RETURN_DISPLAY_PICTURE, "Returned");
if (resultCode == RESULT_OK && requestCode == CAMERA_CAPTURE) {
picUri = data.getData();
performCrop(picUri);
} else if (requestCode == PIC_CROP) {
Bundle extras = data.getExtras();
bitmap = extras.getParcelable("data");
displayPicture.setImageBitmap(bitmap);
}
}
private void performCrop(Uri picUri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
} catch (ActivityNotFoundException e) {
String msg = "Your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toast.show();
}
}
First, Android does not have a CROP Intent.
With respect to the null Uri, it is supposed to be null. Quoting the ACTION_IMAGE_CAPTURE documentation:
The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field.
You do not have EXTRA_OUTPUT, and so you are supposed to get your image via the poorly-documented (Bitmap)data.getExtras().get("data");.
I have an app where at an activity I am taking a photo (among other things) .
Now, when I press the button to take the photo it opens the camera.If i will press the back button or the cancel button (not taking photo) ,it crashes and gives
nullpointer
and
Failure delivering result ResultInfo
in this line:
Bitmap photo = (Bitmap) data.getExtras().get("data");
I use:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_REQUEST){
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
blobvalue = stream.toByteArray();
Bundle extras = new Bundle();
Intent k=new Intent(this,MainActivity.class);
extras.putParcelable("Bitmap", photo);
k.putExtras(extras);
}
if (requestCode == RESULT_CANCELED) {
}
}
and in my adapter:
ImageView myImage=(ImageView) convertView.findViewById(R.id.myimage);
final Bitmap image;
if(theItems.getImagemyItems() != null)
{
byte []temp = theItems.getImagemyItems();
image = BitmapFactory.decodeByteArray(temp, 0, temp.length);
myImage.setImageBitmap(image);
}
else
{
image = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
myImage.setImageBitmap(image);
}
As far as I remember , the above used to workd for this purpose.
I don't know what else to do.
You have just tested requestCode but haven't resultCode so I would suggest you to check resultCode whether user has captured image or cancel capturing.
Try:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST){
if (resultCode == Activity.RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
}
else if (resultCode == Activity.RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
You just have to place a check in your onActivityResult , the case RESULT_OK is when the user takes the picture successfully and the case RESULT_CANCELLED is when you press the hardware back button and want to return to your activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_REQUEST){
if(resultCode == RESULT_OK){
// your code comes here
}
if(resultCode == RESULT_CANCELED){
}
}
}