Android photos upload photos (new)

Source: Internet
Author: User

I wrote an article about photo upload some time ago, but later I found that the images uploaded in that way were processed by the android system, not the original image, that is, compressed images, the image will become very small. Today I am trying to solve this problem in another way.

First, when we want to obtain the original photo, we must specify the path address for storing the photo after taking the photo. This address is specified in Intent by using intent. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (file ));

File is the local output file of the mobile phone, which must be generated before the input parameter. Otherwise, FileNotFoundException will be reported. The following is the source code:

[Java]
DestoryBimap ();
String state = Environment. getExternalStorageState ();
If (state. equals (Environment. MEDIA_MOUNTED )){
String saveDir = Environment. getExternalStorageDirectory ()
+ "/Temple ";
File dir = new File (saveDir );
If (! Dir. exists ()){
Dir. mkdir ();
}
File = new File (saveDir, "temp.jpg ");
File. delete ();
If (! File. exists ()){
Try {
File. createNewFile ();
} Catch (IOException e ){
E. printStackTrace ();
Toast. makeText (ReportSurveyPointActivity. this,
R. string. photo_file_isnull,
Toast. LENGTH_LONG). show ();
Return;
}
}
Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE );
Intent. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (file ));
StartActivityForResult (intent, REQUEST_CODE );
} Else {
Toast. makeText (ReportSurveyPointActivity. this,
R. string. common_msg_nosdcard, Toast. LENGTH_LONG)
. Show ();
}
Here, the destoryBimap () method has already been introduced in the previous article, in order to clear the picture from the phone memory, otherwise the Black will easily report a memory overflow exception, after all, the original picture is relatively large. The following is to determine whether the mobile phone is loaded with SDCard, and then generate the address for storing our photos on the SDCard.

The next step is to load our photo in the onActivityResult () method. It is very easy to load the photo, because we have already assigned the photo path before taking the photo, we can load the photo in this path. The following is the source code:

[Java]
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
If (requestCode = REQUEST_CODE ){
If (resultCode! = RESULT_ OK ){
Return;
}
If (file! = Null & file. exists ()){
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inSampleSize = 2;
Photo = BitmapFactory. decodeFile (file. getPath (), options );
Photo_image.setBackgroundDrawable (new BitmapDrawable (photo ));
PictureDir = file. getPath ();
} Else {
Toast. makeText (this, R. string. photo_file_isnull,
Toast. LENGTH_LONG). show ();
}
}
}
Here, we need to note that when we want to preview a photo in the program, because the photo is large, considering the memory problem, we need to compress the photo and put it in the control, to compress a photo, use BitmapFactory. the inSampleSize attribute of the Options class indicates the proportion of the original photo size to display the photo, that is, the percentage. Here I set 2, that is to say, the normal size of the original photo is displayed.

The following describes how to destroy a photo:

[Java]
/**
* Destroy image files
*/
Private void destoryBimap (){
If (photo! = Null &&! Photo. isRecycled ()){
Photo. recycle ();
Photo = null;
}
}
This method is called both before and after taking a picture and in the onDestroy () method. It is considered in the memory. If you are a dialog, we recommend that you call this method in dimisslistener.

As for the methods that people often ask to upload photos to the background, I also use HttpClient to implement it very easily, and the background can be any framework, the following describes how to upload a photo:

[Java]
/**
* The submission parameter contains file data.
*
* @ Param url
* Server address
* @ Param
* Parameters
* @ Return the result returned by the server
* @ Throws Exception
*/
Public static String uploadSubmit (String url, Map <String, String> param,
File file) throws Exception {
HttpPost post = new HttpPost (url );
 
MultipartEntity entity = new MultipartEntity ();
If (param! = Null &&! Param. isEmpty ()){
For (Map. Entry <String, String> entry: param. entrySet ()){
If (entry. getValue ()! = Null
& Entry. getValue (). trim (). length ()> 0 ){
Entity. addPart (entry. getKey (),
New StringBody (entry. getValue ()));
}
}
}
// Add file Parameters
If (file! = Null & file. exists ()){
Entity. addPart ("file", new FileBody (file ));
}
Post. setEntity (entity );
HttpResponse response = httpClient.exe cute (post );
Int stateCode = response. getStatusLine (). getStatusCode ();
StringBuffer sb = new StringBuffer ();
If (stateCode = HttpStatus. SC _ OK ){
HttpEntity result = response. getEntity ();
If (result! = Null ){
InputStream is = result. getContent ();
BufferedReader br = new BufferedReader (
New InputStreamReader (is ));
String tempLine;
While (tempLine = br. readLine ())! = Null ){
Sb. append (tempLine );
}
} Www.2cto.com
}
Post. abort ();
Return sb. toString ();
}


Author: yaoyeyzq

Related Article

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.