Intent calls the system photo program, and the returned image is too small.

Source: Internet
Author: User
Tags dateformat

Previously Used Method (the returned photo will be compressed and cannot achieve the expected effect ):

Intent intent = new intent ();
Intent intent_camera = getpackagemanager ()
. Getlaunchintentforpackage ("com. Android. Camera ");
If (intent_camera! = NULL ){
Intent. setpackage ("com. Android. Camera ");
}
Intent. setaction (mediastore. action_image_capture );
Startactivityforresult (intent, 100 );


Callback: (data in the returned data area is saved, and then the path is obtained, so that the image is compressed)

@ Override
Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
Log. D ("tag", "requestcode" + requestcode + ", resultcode" + resultcode
+ Data );
If (requestcode = 100 & resultcode = activity. result_ OK
& Null! = Data ){
// Photo return Interface
String sdstate = environment. getexternalstoragestate ();
If (! Sdstate. Equals (environment. media_mounted )){
Toast. maketext (getapplicationcontext (), "SDK not found ",
Toast. length_long). Show ();
Log. D ("tag", "SD card unmount ");
Return;
}
New dateformat ();
String name = dateformat. Format ("yyyymmdd_hhmmss ",
Calendar. getinstance (locale. China ))
+ ". Jpg ";
Log. D ("tag", "name..." + name );
Bundle bundle = data. getextras ();
// Obtain the data returned by the camera and convert it to the image format
Bitmap bitmap;
String filename = NULL;
Bitmap = (Bitmap) bundle. Get ("data ");
Fileoutputstream fout = NULL;
// Define the file storage path
File file = new file ("/sdcard/e_community /");
If (! File. exists ()){
File. mkdirs ();
}
Filename = file. getpath () + "/" + name;
Try {
Fout = new fileoutputstream (filename );
// Compress the image
Bitmap. Compress (fig. compressformat. PNG, 100, fout );
// Fileoutputstream outputstream = new fileoutputstream (filename); // file output stream
// Outputstream. Write (data); // write data to the SD card
// Outputstream. Close (); // close the output stream
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Finally {
Try {
Fout. Flush ();
Fout. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
Log. D ("tag", "Photo path" + filename );

}



To solve the problem that the image is too small, it is to set the path and name of the photo when starting the camera, so that data does not return data but is null, in this way, you can use bitmapfactory Based on the configured path. decodefile (imageurl. get (J), option); get the photo.

The startup code is as follows:

Private string camera_path = environment. getexternalstoragedirectory (). tostring () + "/e_community/"; saved path

Private string camera_photo_name; // saved name


Uri moutputfileuri;

Intent intent = new intent (mediastore. action_image_capture );
File path1 = new file (photoutil. camera_path );
If (! Path1.exists ()){
Path1.mkdirs ();
}
Camera_photo_name = system. currenttimemillis () + ". PNG ";
File file = new file (path1, camera_photo_name );
Moutputfileuri = URI. fromfile (File );
Intent. putextra (mediastore. extra_output, moutputfileuri );

Startactivityforresult (intent, 111 );


Callback:

@ Override
Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
Log. D ("tag", "requestcode" + requestcode + ", resultcode" + resultcode
+ Data );
If (requestcode = 111 & resultcode = activity. result_ OK ){
// Photo return Interface
String filename = photoutil. camera_path + "/" + camera_photo_name;
Log. D ("tag", "Photo path" + filename );

}

}



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.