简体   繁体   中英

Summary: Take a picture utilizing Camera Intent and display the photo with correct orientation (works on hopefully all devices)

It seems to be the simplest thing in the world: taking a picture within your Android app using the default camera activity. However, there are many pitfalls which are covered in several posts across StackOverflow and the web as, for instance, Null Intents being passed back, the orientation of the picture not being correct or OutOfMemoryErrors.

I'm looking for a solution that allows me to

  1. start the camera activity via the camera intent,
  2. retrieve the Uri of the photo, and
  3. retrieve the correct orientation of the photo.

Moreover, I would like to avoid a device configuration (manufacturer, model, os version) specific implementation as far as possible. So I'm wondering: what is the best way to achieve this?

UPDATE: January 2nd, 2014: I tried really hard to avoid implementing different strategies based on the device manufacturer. Unfortunately, I did not get around it. Going through hundreds of posts and talking to several developers, nobody found a solution that works on all devices without implementing device manufacturer specific code.

After I posted my solution here on StackOverflow, some developers asked me to publish my code on github. So here it is now: AndroidCameraUtil on github

The code was successfully tested on a wide variety of devices with Android API-Level >= 8. For a complete list, please see the Readme file on github.

The CameraIntentHelperActivity provides the main functionality, which is also described in more detail in the following.

Calling the default camera activity:

  • for Samsung and Sony devices : I call the camera activity with the method call to startActivityForResult. I only set the constant CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE. I do NOT set any other intent extras.
  • for all other devices : I call the camera activity with the method call to startActivityForResult as previously. This time, however, I additionally set the intent extra MediaStore.EXTRA_OUTPUT and provide an URI, where I want the image to be stored.

In both cases I remember the time the camera activity was started.


On camera activity result:

  1. Mediastore: First, I try to read the photo being captured from the MediaStore. Using a mangedQuery on the MediaStore content, I retrieve the latest image being taken, as well as its orientation property and its timestamp. If I find an image and it was not taken before the camera intent was called, it is the image I was looking for. Otherwise, I dismiss the result and try one of the following approaches.
  2. Intent extra: Second, I try to get an image Uri from intent.getData() of the returning intent. If this is not successful either, I continue with step 3.
  3. Default photo Uri : If all of the above mentioned steps did not work, I use the image Uri I passed to the camera activity.

At this point, I retrieved the photo Uri and its orientation which I pass to my UploadPhotoActivity.


Image processing

Please take a close look at my BitmapHelper class. It is based on the code described in detail in that tutorial .

Moreover, the shrinkBitmap method also rotates the image if required based on the orientation information extracted earlier.


I hope this is helpful to some of you.

I have tested this code with a Sony Xperia Go, Samsung Galaxy SII, Samsung Galaxy SIII mini and a Samsung Galaxy Y it worked on all devices!

But on the LG E400 (2.3.6) it didn't work and you get double pictures in the gallery. So i have added the manufacturer.contains("lge") in the void startCameraIntent() and it fixed the problem.

if(!(manufacturer.contains("samsung")) && !(manufacturer.contains("sony")) && !(manufacturer.contains("lge"))) {
    String filename = System.currentTimeMillis() + ".jpg";
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, filename);
    cameraPicUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraPicUri);
}

On a Galaxy S3 with CM 10.1 I get a nullpointer exception in BitmapHelper:

bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);

subsequently my UploadPhotoActivity fails at:

  try { photo = BitmapHelper.readBitmap(this, cameraPicUri); if (photo != null) { photo = BitmapHelper.shrinkBitmap(photo, 600, rotateXDegrees); thumbnail = BitmapHelper.shrinkBitmap(photo, 100); ImageView imageView = (ImageView) findViewById(R.id.sustainable_action_photo); imageView.setImageBitmap(photo); } else { Log.e(TAG,"IMAGE ERROR 1"); } } catch (Exception e) { Log.e(TAG,"IMAGE ERROR 2"); e.printStackTrace(); } 

at the second log (IMAGE ERROR 2). After a couple of tries my camera broke and I got a "Could not connect to camera"-error.

Tested it on a nexus 7 and it works perfectly.

Edit: Narrowed it down to this:

fileDescriptor = context.getContentResolver().openAssetFileDescriptor(selectedImage, "r");

Although selectedImage contains this:

file:///storage/emulated/0/DCIM/Camera/IMG_20131023_183343.jpg

The fileDescriptor returns a FileNotFoundException. I checked the file system and the image is not saved at this location. The cameraPicUri in TakePhotoActivity points to a non existant image. I am currently checking where it all goes wrong.

Edit2: I figured out the error: Since the device is a Samsung, and tells the App that it is a Samsung device, your Samsung specific fixes are applied. Cyanogenmod does not need those fixes though, and in the end the code breaks. Once you remove

(manufacturer.contains("samsung")) &&

It works. Since this is a custom ROM you could not plan for that of course. I am trying to figure out a way to detect if the device is running cyanogenmod and then include this in your code.

Thanks for a nice camera fix!

Edit3: I fixed it to run on Cyanogenmod on the Galaxy S3 by changing your code to this: Well, now it sometimes works, sometimes it does not. Strange.

if (getPackageManager().hasSystemFeature("com.cyanogenmod.android") || (!(manufacturer.contains("samsung")) && !(manufacturer.contains("sony")) && !(manufacturer.contains("lge"))))

I experience some problems when using this with Sony Xperia Z5.

I added this and it got a lot better.

if (buildType.contains("sony")&& buildDevice.contains("e5823")) {
                setPreDefinedCameraUri = true;}

But 4 times out of 22 it restarted the camera and once it restarted two times. I restarted the App due to every test. Is there some way to get around this or do I accept this result?

The thing is that if the camera restarts I can press the back button twice and boom, the image is there in my Imageview and saved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM