This is the call camera
public static File Getimagefromcamer (context context, File camerafile,
int reque_code_camera, Intent Intent) {
Intent = new Intent (mediastore.action_image_capture);
File Filedir = helputil.getfile (Context, "/tour/user_photos");
Camerafile = new File (filedir.getabsolutefile () + "/"
+ system.currenttimemillis () + ". jpg");
Intent.putextra (Mediastore.extra_output, Uri.fromfile (Camerafile));
(activity)-Startactivityforresult (Intent, reque_code_camera);
return camerafile;
}
Here I returned a file object, which should be required for the project, you can not have to write it directly, pass a URI object.
Here is the call album
public static void Getimagefromphoto (context context, int reque_code_photo) {
Intent Intent = new Intent (intent.actio N_pick, null);
Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
(activity) (Startactivityforresult). (Intent, Reque_code_photo);
}
Of course, the next step is to invoke the onactivityresult of activity.
@Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = = Result_ok
{switch (requestcode) {Case ConstantUtil.REQUE_CODE_CAMERA:uri = Uri.fromfile (camerafile);
Photoutil.startphotozoom (context, URI, Constantutil.reque_code_crop);
Break
Case ConstantUtil.REQUE_CODE_PHOTO:if (null!= data) {//to deselect the URI = Data.getdata () for which no null pointer is selected;
Photoutil.startphotozoom (context, URI, Constantutil.reque_code_crop);
} break;
Case ConstantUtil.REQUE_CODE_CROP:if (uri==null) {break;
} Cropbitmap=helputil.getbitmapfromuri (Uri,context);
if (Cropbitmap!= null) {Iv_headphoto.setimagebitmap (CROPBITMAP);
BAOs = new Bytearrayoutputstream ();
Cropbitmap.compress (Bitmap.CompressFormat.JPEG, BAOs);
headpicstring = new String (Base64.encode (Baos.tobytearray (), 0));
Uploadpic (headpicstring);
} break;
Default:break; }
}
And, of course, the cuts that everyone cares about.
public static void Startphotozoom (context context, URI Uri,
int reque_code_crop) {
int dp =;
Intent Intent = new Intent ("Com.android.camera.action.CROP");
Intent.setdataandtype (URI, "image/*");
The following crop=true is set to set the view trimmed
intent.putextra ("Crop", "true")
displayed in the open intent. Intent.putextra ("scale", true);//Go to the black side
Intent.putextra ("scaleupifneeded", true);//Go to the Black side
//Aspectx Aspecty is a wide-high proportional
Intent.putextra ("Aspectx", 1);//output is the X-directional ratio
Intent.putextra ("Aspecty", 1);
Outputx outputy is cropped picture width high, should not change the following numbers, will be stuck dead
Intent.putextra ("Outputx", DP);//output X-directional pixel
Intent.putextra (" Outputy ", DP);
Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ());
Intent.putextra ("Nofacedetection", true);
Intent.putextra (Mediastore.extra_output, URI);
Intent.putextra ("Return-data", false);//set to not return data
(activity) context. Startactivityforresult (Intent, Reque_ Code_crop);
}
In many blogs, "Return-data" is set to true and the data is obtained by Data.getparcelableextra ("data") in Onactivityresult, but the value of the DP variable cannot be too large, Otherwise, your program is dead. This is where I have problems. In most high distribution phones this use is no problem, but a lot of low with mobile phone is a bit hold, directly on the anomaly, including our domestic God machine Rice 3 also did not hold live, so I suggest you do not pass the return data large numbers, Small data is still no problem, saying that we use the URI as much as possible to help us when we cut the picture.
Here are some of the parameters we use to trim
Exta Options Table for image/* crop:
Setextra |
DataType |
Description |
Crop |
String |
Signals the crop feature |
Aspectx |
Int |
Aspect Ratio |
Aspecty |
Int |
Aspect Ratio |
Outputx |
Int |
Width of output created from this Intent |
Outputy |
Int |
Width of output created from this Intent |
Scale |
Boolean |
Should it scale |
Return-data |
Boolean |
Return the bitmap and action=inline-data by using the data |
Data |
Parcelable |
Bitmap to process, your may provide it a Bitmap (not tested) |
Circlecrop |
String |
If this string is not NULL, it would provide some circular CR |
Mediastore.extra_output ("OUTPUT") |
Uri |
Set this URi to a File:///, example code |
Finally, the way to get bitmap through the URI for everyone to paste
public static Bitmap Getbitmapfromuri (URI Uri,context mcontext) {try {//Read the picture of the URI
Bitmap Bitmap = MediaStore.Images.Media.getBitmap (Mcontext.getcontentresolver (), URI);
return bitmap;
catch (Exception e) {e.printstacktrace ();
return null; }
}