Cropping uploaded images and image Cropping
For an application that involves user information, user images must be set. This was just a simple copy and paste before, which caused many problems during the recent redo of this function. After the photo and album were selected, the system was started to crop the image or the system was not crashed or no data was returned, let's take a look at this feature today.
Call the system photo interface:
Private void startTakePhotoActivity () {temp_file_name = String. valueOf (System. currentTimeMillis () + FILE_SUFFIX; temp_photo_uri = Uri. fromFile (new File (DIRECTORY, temp_file_name); Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); // By default, you do not need to specify the uri of the image generated by the photo. The system returns the image thumbnail uri by default. // If you specify the uri of the image generated, the system will not return the image.Intent. putExtra (MediaStore. EXTRA_OUTPUT, temp_photo_uri );StartActivityForResult (intent, FLAG_CAMERA );}
Call the system album interface:
Private void startPickPhotoActivity () {// temp_file_name is not assigned a value here to initialize temp_file_name = System. currentTimeMillis () + FILE_SUFFIX; Intent intent = new Intent (Intent. ACTION_PICK, android. provider. mediaStore. images. media. EXTERNAL_CONTENT_URI); intent. setDataAndType (MediaStore. images. media. EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult (intent, FLAG_GALLERY );}
I would like to add that many netizens have said what's wrong with android4.4 or above. In fact, you don't have to think so much about it. Is it still the same as before? We can see that some netizens have made judgments for Versions later than 4.4 and earlier. This is only because Versions later than 4.4 provide another intent when selecting images from the album.Intent. ACTION_OPEN_DOCUMENT,You can use recently browsed images, but the amount of code is large. Therefore, I personally think that it is not necessary to make such a complex choice of images, so I still use the album to select images.
Call the system cropping interface:
Private void startCropPhotoActivity (Uri orgUri) {// Save the uri String _ fileName = "_" + System. currentTimeMillis () + ". jpg "; temp_photo_uri = Uri. fromFile (new File (DIRECTORY, _ fileName); // crop the image Intent intent Intent = new Intent ("com. android. camera. action. CROP "); intent. setDataAndType (orgUri, "image/*"); intent. putExtra ("crop", "true"); intent. putExtra ("aspectX", 1); intent. putExtra ("aspectY", 1); intent. putExtra ("outputX", 640); intent. putExtra ("outputY", 640); intent. putExtra ("scale", true); // Save the cut image to the target Uri.Intent. putExtra (MediaStore. EXTRA_OUTPUT, temp_photo_uri );Intent. putExtra ("return-data", false); intent. putExtra ("outputFormat", Bitmap. compressFormat. JPEG); intent. putExtra ("noFaceDetection", true); startActivityForResult (intent, FLAG_CROP );}
Although the Code has been annotated, I still need to read the bold red code above with everyone. I didn't have a good understanding before, which leads to a big heel here.
Intent. putExtra (MediaStore. EXTRA_OUTPUT, temp_photo_uri): used to specify the storage location of the image generated after the photo or cropping. If this parameter is specified, the Uri of the thumbnail generated will not be returned after the image is taken, and intent will be used. getData is bound to have NullPointException. In this case, you need to use Uri to store the Uri from the file or simply create a member variable.
The problem has basically been solved here, but now it is a cloud era, most of the ROM after the android system personality, the album function looks very powerful, but it brings endless pain to developers. Let's talk about the problems caused by this cloud album.
Return to my code and take a photo. It is OK, that is, the resultCode is always 0 (normal RESULT_ OK =-1) After selecting an image from the album is returned in onActivityResult ), after comparing the returned Uri, we found that the problem was not that the Uri format was incorrect, but that the selected photo was not found on the tested mobile phone. But why is it still shown here, and the returned Uri looks no different from the local photo? These third-party vendors have dug such a big pitfall for developers. All right, the problem can be solved. Since the picture does not exist, we have to let the user know that the selected picture is not on the mobile phone. This avoids program crashes and improves the user experience, if you have the strength, you can choose not to use the system album so that the above mentioned problem will not occur.
After talking so much, it seems like a basket of nonsense. In response to this problem, I have referred to the Code provided by several netizens. There are many things worth learning from, and I will share them with you, cameraActivity was modified by me and passed the test on android4.4 and 4.1. Encapsulate a tool class when you are free.
Code download: http://pan.baidu.com/s/1miq120g