Android photos upload photos

Source: Internet
Author: User

Not to mention nonsense, go to the theme directly. The easiest way to take a photo in android is to set the Action to android in a New Intent. media. action. IMAGE_CAPTURE then uses startActivityForResult (intent, REQUEST_CODE) to enter the camera. There are also many ways to achieve this. You can search online. However, before entering the camera, it is best to determine whether the sdcard is available. The Code is as follows:
View Source Code
 
Print
1 destoryBimap ();
2 String state = Environment. getExternalStorageState ();
3 if (state. equals (Environment. MEDIA_MOUNTED )){
4 intent = new Intent ("android. media. action. IMAGE_CAPTURE ");
5 startActivityForResult (intent, REQUEST_CODE );
6} else {
7 Toast. makeText (DefectManagerActivity. this,
8 R. string. common_msg_nosdcard, Toast. LENGTH_LONG). show ();
9}
After the image is taken, you need to obtain the image in the onActivityResult (int requestCode, int resultCode, Intent data) method. android encapsulates the image to bundle and passes it back, however, the method for obtaining photos from different machines is not the same, so there may be an imaginary way of getting images as null. The solution is to make a judgment. If one method cannot be used, in another way, the following two methods are used to obtain photos:
View Source Code
 
Print
01 Uri uri = data. getData ();
02 if (uri! = Null ){
03 photo = BitmapFactory. decodeFile (uri. getPath ());
04}
05 if (photo = null ){
06 Bundle bundle = data. getExtras ();
07 if (bundle! = Null ){
08 photo = (Bitmap) bundle. get ("data ");
09} else {
10 Toast. makeText (DefectManagerActivity. this,
11 getString (R. string. common_msg_get_photo_failure ),
12 Toast. LENGTH_LONG). show ();
13 return;
14}
15}
The first method is to call getData () with the intent returned from the method. The method obtains the data Uri and then obtains the data path based on the uri, then, encapsulate it into a bitmap based on the path.
The second method is the intent object returned in the usage, but it is no longer calling getData (); the method is calling getExtras (); The method gets a bundle of an object set of all parameters in the intent, then we use the bundle object to get the value of the key as data, that is, a bitmap object.
The above two methods can be used to obtain the bitmap object of a photo, and then it can be used in the program. If you want to save the photo to your designated directory, you can use the following steps:
First, bitmap has a compress (Bitmap. compressFormat. JPEG, 100, baos) method. This method has three parameters: the first parameter is the format of the image to be saved, and the second parameter is the quality of the image to be saved. The value is 0-100, for example, you can set this parameter for images in PNG format, because PNG is a lossless format. The third parameter is a buffer output stream ByteArrayOutputStream (). The function of this method is to convert the bitmap image to the jpge format and put it into the output stream, then you should understand how to operate it. The following is the instance code:
View Source Code
 
Print
01 String pictureDir = "";
02 FileOutputStream fos = null;
03 BufferedOutputStream bos = null;
04 ByteArrayOutputStream baos = null;
05 try {
06 baos = new ByteArrayOutputStream ();
07 bitmap. compress (Bitmap. CompressFormat. JPEG, 100, baos );
08 byte [] byteArray = baos. toByteArray ();
09 String saveDir = Environment. getExternalStorageDirectory ()
10 + "/temple ";
11 File dir = new File (saveDir );
12 if (! Dir. exists ()){
13 dir. mkdir ();
14}
15 File file = new File (saveDir, "temp.jpg ");
16 file. delete ();
17 if (! File. exists ()){
18 file. createNewFile ();
19}
20 fos = new FileOutputStream (file );
21 bos = new BufferedOutputStream (fos );
22 bos. write (byteArray );
23 pictureDir = file. getPath ();
24} catch (Exception e ){
25 e. printStackTrace ();
26} finally {
27 if (baos! = Null ){
28 try {
29 baos. close ();
30} catch (Exception e ){
31 e. printStackTrace ();
32}
33}
34 if (bos! = Null ){
35 try {
36 bos. close ();
37} catch (Exception e ){
38 e. printStackTrace ();
39}
40}
41 if (fos! = Null ){
42 try {
43 fos. close ();
44} catch (Exception e ){
45 e. printStackTrace ();
46}
47}
48}
Then we can implement the image upload function. Here we use the MultipartEntity in the HttpClient of apache to implement File Upload. The specific code is as follows:
View Source Code
 
Print
01 /**
02 * file data is included in the submission Parameters
03 *
04 * @ param url
05 * server address
06 * @ param
07 * Parameters
08 * @ return server return result
09 * @ throws Exception
10 */
11 public static String uploadSubmit (String url, Map <String, String> param,
12 File file) throws Exception {
13 HttpPost post = new HttpPost (url );
14
15 MultipartEntity entity = new MultipartEntity ();
16 if (param! = Null &&! Param. isEmpty ()){
17 for (Map. Entry <String, String> entry: param. entrySet ()){
18 entity. addPart (entry. getKey (), new StringBody (entry. getValue ()));
19}
20}
21 // Add file Parameters
22 if (file! = Null & file. exists ()){
23 entity. addPart ("file", new FileBody (file ));
24}
25 post. setEntity (entity );
26 HttpResponse response = httpClient.exe cute (post );
27 int stateCode = response. getStatusLine (). getStatusCode ();
28 StringBuffer sb = new StringBuffer ();
29 if (stateCode = HttpStatus. SC _ OK ){
30 HttpEntity result = response. getEntity ();
31 if (result! = Null ){
32 InputStream is = result. getContent ();
33 BufferedReader br = new BufferedReader (
34 new InputStreamReader (is ));
35 String tempLine;
36 while (tempLine = br. readLine ())! = Null ){
37 sb. append (tempLine );
38}
39}
40}
41 post. abort ();
42 return sb. toString ();
43}
This is basically the case for uploading images, but the problem is that bitmap is still in memory after the images are uploaded. We all know that if the images in high definition are large, however, the mobile phone memory is limited. If no processing is performed, it is easy to report memory overflow. Therefore, we should release the processed bitmap from the memory, at this time, you need to call the recycle () of bitmap. When calling this method, you must be aware that it cannot be too early or too late. Otherwise, an exception will be reported, generally, it can be placed before the next image is generated or when no view references the image to be destroyed. The following is the instance code:
View Source Code
 
Print
1 /**
2 * destroy image files
3 */
4 private void destoryBimap (){
5 if (photo! = Null &&! Photo. isRecycled ()){
6 photo. recycle ();
7 photo = null;
8}
9}

 

From Yeju dongli
 

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.