Image upload (i)

Source: Internet
Author: User

Application scenario: Uploading pictures or uploading photos

The actual situation is changed according to personal circumstances.

On the code:

Mainactivity:

Package com.example.one2012;

Import Java.util.HashMap;
Import Java.util.Map;

Import android.app.Activity;
Import Android.app.ProgressDialog;
Import android.content.Intent;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.ImageView;
Import Android.widget.ProgressBar;
Import Android.widget.TextView;
Import Android.widget.Toast;

Import Com.example.one2012.UploadUtil.OnUploadProcessListener;

public class Mainactivity extends Activity implements Onclicklistener,
Onuploadprocesslistener {
private static final String TAG = "Uploadimage";

/**
* To upload files
*/
protected static final int to_upload_file = 1;
/**
* Upload file response
*/
protected static final int upload_file_done = 2; //
/**
* Select File
*/
public static final int to_select_photo = 3;
/**
* Upload Initialization
*/
private static final int upload_init_process = 4;
/**
* Upper Crossing
*/
private static final int upload_in_process = 5;
/***
* This URL here is the Java EE Environment URL for my server
*/
private static String Requesturl = "Http://192.168.10.160:8080/fileUpload/p/file!upload";

Private Button Selectbutton, Uploadbutton;
Private ImageView ImageView;
Private TextView Uploadimageresult;
Private ProgressBar ProgressBar;

Private String Picpath = null;
Private ProgressDialog ProgressDialog;

/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initview ();
}

/**
* Initialize Data
*/
private void Initview () {
Selectbutton = (Button) This.findviewbyid (r.id.selectimage);
Uploadbutton = (Button) This.findviewbyid (r.id.uploadimage);
Selectbutton.setonclicklistener (this);
Uploadbutton.setonclicklistener (this);
ImageView = (ImageView) This.findviewbyid (R.id.imageview);
Uploadimageresult = (TextView) Findviewbyid (R.id.uploadimageresult);
ProgressDialog = new ProgressDialog (this);
ProgressBar = (ProgressBar) Findviewbyid (R.ID.PROGRESSBAR1);
}

@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.selectimage:
Intent Intent = new Intent (this, selectpicactivity.class);
Startactivityforresult (Intent, To_select_photo);
Break
Case R.id.uploadimage:
if (Picpath! = null) {
LOG.V ("Get to the picture path is:::", Picpath);
Handler.sendemptymessage (To_upload_file);
} else {
Toast.maketext (This, "Error uploading file path", Toast.length_long). Show ();
}
Break
Default
Break
}
}

@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
if (ResultCode = = Activity.result_ok && Requestcode = = To_select_photo) {
Picpath = Data.getstringextra (Selectpicactivity.key_photo_path);
LOG.I (TAG, "final Selected picture =" + Picpath);
Bitmap BM = bitmapfactory.decodefile (Picpath);
Imageview.setimagebitmap (BM);
}
Super.onactivityresult (Requestcode, ResultCode, data);
}

/**
* Upload Server Response callback
*/
@Override
public void Onuploaddone (int responsecode, String message) {
Progressdialog.dismiss ();
Message msg = Message.obtain ();
Msg.what = Upload_file_done;
MSG.ARG1 = Responsecode;
msg.obj = message;
Handler.sendmessage (msg);
}

private void Touploadfile () {
Uploadimageresult.settext ("uploading ...");
Progressdialog.setmessage ("Uploading Files ...");
Progressdialog.show ();

String Filekey = "pic";
Uploadutil uploadutil = Uploadutil.getinstance ();
Uploadutil.setonuploadprocesslistener (this); Set Listener listening upload status

map<string, string> params = new hashmap<string, string> ();
Params.put ("OrderId", "11111");
Uploadutil.uploadfile (Picpath, Filekey, Requesturl, params);
}

Private Handler Handler = new Handler () {
@Override
public void Handlemessage (Message msg) {
Switch (msg.what) {
Case To_upload_file:
Touploadfile ();
Break

Case Upload_init_process:
Progressbar.setmax (MSG.ARG1);
Break
Case Upload_in_process:
Progressbar.setprogress (MSG.ARG1);
Break
Case Upload_file_done:
String result = "Response code:" + msg.arg1 + "\ n Response message:" + msg.obj
+ "\ n Time:" + uploadutil.getrequesttime () + "seconds";
Uploadimageresult.settext (result);
Break
Default
Break
}
Super.handlemessage (msg);
}

};

@Override
public void onuploadprocess (int uploadsize) {
Message msg = Message.obtain ();
Msg.what = upload_in_process;
MSG.ARG1 = uploadsize;
Handler.sendmessage (msg);
}

@Override
public void initupload (int fileSize) {
Message msg = Message.obtain ();
Msg.what = upload_init_process;
MSG.ARG1 = fileSize;
Handler.sendmessage (msg);
}

}

========

Selectpictureactivity:

Package com.example.one2012;

Import android.app.Activity;
Import android.content.ContentValues;
Import android.content.Intent;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.provider.MediaStore;
Import Android.util.Log;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.LinearLayout;
Import Android.widget.Toast;
Import android.app.Activity;
Import android.content.ContentValues;
Import android.content.Intent;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.provider.MediaStore;
Import Android.util.Log;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.LinearLayout;
Import Android.widget.Toast;

/**
* @author Spring sky<br>
Description: Primarily used to select file operations
*/

public class Selectpicactivity extends Activity implements Onclicklistener {

/***
* Take photos with camera to get pictures
*/
public static final int select_pic_by_tack_photo = 1;
/***
* Use the pictures in the album
*/
public static final int select_pic_by_pick_photo = 2;

/***
* Get the image path key from intent
*/
public static final String Key_photo_path = "Photo_path";

private static final String TAG = "selectpicactivity";

Private LinearLayout dialoglayout;
Private Button takephotobtn, pickphotobtn, cancelbtn;

/** get to the picture path */
Private String Picpath;

Private Intent lastintent;

Private Uri Photouri;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.select_pic_layout);
Initview ();
}
/**
* Initialize load View
*/
private void Initview () {
Dialoglayout = (linearlayout) Findviewbyid (r.id.dialog_layout);
Dialoglayout.setonclicklistener (this);
TAKEPHOTOBTN = (Button) Findviewbyid (R.id.btn_take_photo);
Takephotobtn.setonclicklistener (this);
PICKPHOTOBTN = (Button) Findviewbyid (R.id.btn_pick_photo);
Pickphotobtn.setonclicklistener (this);
CANCELBTN = (Button) Findviewbyid (r.id.btn_cancel);
Cancelbtn.setonclicklistener (this);
Lastintent = Getintent ();
}

@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.dialog_layout:
Finish ();
Break
Case R.id.btn_take_photo:
Takephoto ();
Break
Case R.id.btn_pick_photo:
Pickphoto ();
Break
Default
Finish ();
Break
}
}

/**
* Take photos to get pictures
*/
private void Takephoto () {
Before performing a photo shoot, you should first determine if the SD card exists
String sdstate = Environment.getexternalstoragestate ();
if (Sdstate.equals (environment.media_mounted)) {

Intent Intent = new Intent (mediastore.action_image_capture);//"Android.media.action.IMAGE_CAPTURE"
/***
* Need to explain, the following operation using a camera to take pictures, photos after the picture will be stored in the album in this way there is a good thing is to get the picture is the original image after the photo
* If it is not practical contentvalues store photo path, photos taken after the picture is not clear image thumbnail
*/
Contentvalues values = new Contentvalues ();
Photouri = This.getcontentresolver (). Insert (
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent.putextra (Android.provider.MediaStore.EXTRA_OUTPUT, Photouri);
/** ----------------- */
Startactivityforresult (Intent, Select_pic_by_tack_photo);
} else {
Toast.maketext (This, "memory card does not exist", Toast.length_long). Show ();
}
}

/***
* Take pictures from the album
*/
private void Pickphoto () {
Intent Intent = new Intent ();
Intent.settype ("image/*");
Intent.setaction (intent.action_get_content);
Startactivityforresult (Intent, Select_pic_by_pick_photo);
}

@Override
public boolean ontouchevent (Motionevent event) {
Finish ();
Return Super.ontouchevent (event);
}

@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
if (ResultCode = = ACTIVITY.RESULT_OK) {
Dophoto (Requestcode, data);
}
Super.onactivityresult (Requestcode, ResultCode, data);
}

/**
* After selecting a picture, get the path to the picture
*
* @param requestcode
* @param data
*/
private void Dophoto (int requestcode, Intent data) {
if (Requestcode = = Select_pic_by_pick_photo)//from the album to take pictures, some phones have abnormal situation, please note
{
if (data = = NULL) {
Toast.maketext (This, "Error selecting picture file", Toast.length_long). Show ();
Return
}
Photouri = Data.getdata ();
if (Photouri = = null) {
Toast.maketext (This, "Error selecting picture file", Toast.length_long). Show ();
Return
}
}
String[] Pojo = {MediaStore.Images.Media.DATA};
cursor cursor = managedquery (Photouri, pojo, NULL, NULL, NULL);
if (cursor! = NULL) {
int columnindex = Cursor.getcolumnindexorthrow (pojo[0]);
Cursor.movetofirst ();
Picpath = cursor.getstring (columnindex);
Cursor.close ();
}
LOG.I (TAG, "ImagePath =" + Picpath);
if (Picpath! = null
&& (Picpath.endswith (". png") | | picpath.endswith (". PNG ")
|| Picpath.endswith (". jpg") | | Picpath.endswith (". JPG ")) {
Lastintent.putextra (Key_photo_path, Picpath);
Setresult (ACTIVITY.RESULT_OK, lastintent);
Finish ();
} else {
Toast.maketext (This, "Select picture file is incorrect", Toast.length_long). Show ();
}
}
}

============

XML files and Uploadutil in the demo, but also pay attention to the issue of permissions ha,

See the full code:

Csdn Upload tomorrow

Image upload (i)

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.