Android calls system camera to take pictures to get the original image

Source: Internet
Author: User

Well, it's been a whole day for the problem. Before the method found on the Internet, if in Onactivityresult directly with the Data.getdata () way to generate bitmap, actually get the photo generated thumbnail! Look at the size and you'll know. If you want to get the original image, it will take some tossing (especially for this Samsung i9000 in hand)--the way my friend used it on different models has been an error in my case, and it belongs to that uncaught mistake-.-In other words, the specific toss-up method is as follows (if unfortunately you see, hope not "painful"):
Write the following code in the Click event of the Start button for the photo:
Tocam.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
First verify that the phone has sdcard
String status=environment.getexternalstoragestate ();
if (Status.equals (environment.media_mounted))
{
try {
File Dir=new file (environment.getexternalstoragedirectory () + "/" +localtempimgdir);
if (!dir.exists ()) dir.mkdirs ();

Intent intent=new Intent (Android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File F=new file (dir, localtempimgfilename);//localtempimgdir and Localtempimagefilename are their own defined names.
Uri U=uri.fromfile (f);
Intent.putextra (MediaStore.Images.Media.ORIENTATION, 0);
Intent.putextra (Mediastore.extra_output, u);
Startactivityforresult (Intent, Get_image_via_camera);
} catch (Activitynotfoundexception e) {
TODO auto-generated Catch block
Toast.maketext (Impromptuactivity.this, "No storage Directory found", Toast.length_long). Show ();
}
}else{
Toast.maketext (Impromptuactivity.this, "No Memory Card", Toast.length_long). Show ();
}
Dialog.dismiss ();
}
});

In the Onactivityresult this toss:
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
if (RESULTCODE==RESULT_OK)
{
Switch (Requestcode)
{
Case Get_image_via_camera:
File F=new file (Environment.getexternalstoragedirectory ()
+ "/" +localtempimgdir+ "/" +localtempimgfilename);
try {
Uri U =
Uri.parse (Android.provider.MediaStore.Images.Media.insertImage (Getcontentresolver (),
F.getabsolutepath (), NULL, NULL));
You are the URI of the original picture taken, and the rest of you want to do God horse bad things go ahead ...
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Break
}
}
Super.onactivityresult (Requestcode, ResultCode, data);
}

Don't be happy, it's too early, and the key question:
The above code as long as you can use Google, generally can find to piece together. However, just like this, you will also get an unknown error ... Samsung's system camera, layout is flat, if your activity happens to be vertical version, then get this callback URI, it is likely to be empty!
The reason is that if you don't set a layout change, the activity doesn't call the OnCreate method! This is where the hell is!
The Setup method is actually simple:
1. In Manfest.xml, add an attribute to the activity: android:configchanges= "Orientation|keyboardhidden"
2. Add in activity:
@Override
public void onconfigurationchanged (Configuration config) {
super.onconfigurationchanged (config);
}
There is also an egg-ache error that is easy to commit: Do not add a singleton mode to this activity, namely: Android:launchmode= "SingleInstance"

Well, finish this, and you'll finally be able to not "break the pain"! Good luck!

Android calls system camera to take pictures to get the original image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.