Android 拍照 以及從本地選擇圖片 上傳

Android 拍照以及選擇本地圖片上傳(使用MX4沒有問題,LG手機貌似不能選擇本地圖片,以後有時間看看),在網上找的一些資料,改寫的。

activity代碼

package com.ytint.wloaa.activity;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ab.activity.AbActivity;
import com.ab.view.ioc.AbIocView;
import com.ytint.wloaa.R;
import com.ytint.wloaa.app.MyApplication;
import com.ytint.wloaa.bean.URLs;

/**
 * 本地上傳和調用系統拍照
 * @author Administrator
 *
 */
public class PhotoActivity  extends AbActivity implements OnClickListener{
	private MyApplication application;
	String TAG = "PhotoActivity";
    private ImageView img;
    private EditText img_content;
    private Button nati;
    private Button pai;
    private Button submit;
    
    @AbIocView(id = R.id.photo_full)
	LinearLayout photo_full;
    
    private static String srcPath;
    private static final int TIME_OUT = 10*1000;   //超時時間
    private static final String CHARSET = "utf-8"; //設置編碼
    private String loginKey;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aa);
        application = (MyApplication) this.getApplication();
	loginKey = application.getProperty("loginKey");
        initView();
    }

    private void initView() {
        img = (ImageView) findViewById(R.id.img);
        nati = (Button) findViewById(R.id.natives);
        pai = (Button) findViewById(R.id.pai);
        submit = (Button) findViewById(R.id.submit);
        img_content=(EditText)findViewById(R.id.img_content);
        nati.setOnClickListener(this);
        pai.setOnClickListener(this);
        submit.setOnClickListener(this);
        
    	OnClickListener keyboard_hide = new OnClickListener() {

			@Override
			public void onClick(View v) {
				InputMethodManager imm = (InputMethodManager) PhotoActivity.this
						.getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
			}

		};
		photo_full.setClickable(true);
		photo_full.setOnClickListener(keyboard_hide);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.natives:
                Intent local = new Intent();
                local.setType("image/*");
                local.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(local, 2);
                break;
            case R.id.pai:
                Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(it, 1);
                break;
            case R.id.submit:
            	if (srcPath == null || srcPath=="") {
            		showToast("文件不存在");
            	}else{
            		submitUploadFile();
            	}
            	break;
        }
    }

    /**
     * 拍照上傳
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) {
            switch(requestCode) {
                case 1:
                    Bundle extras = data.getExtras();
                    Bitmap b = (Bitmap) extras.get("data");
                    img.setImageBitmap(b);
                    String name = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
                    String fileNmae = Environment.getExternalStorageDirectory().toString()+File.separator+"dong/image/"+name+".jpg";
                    srcPath = fileNmae;
                    System.out.println(srcPath+"----------保存路徑1");
                    File myCaptureFile =new File(fileNmae);
                    try {
                        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                            if(!myCaptureFile.getParentFile().exists()){
                                myCaptureFile.getParentFile().mkdirs();
                            }
                            BufferedOutputStream bos;
                            bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
                            b.compress(Bitmap.CompressFormat.JPEG, 80, bos);
                            bos.flush();
                            bos.close();
                        }else{

                            Toast toast= Toast.makeText(PhotoActivity.this, "保存失敗,SD卡無效", Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            toast.show();
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    Uri uri = data.getData();
                    img.setImageURI(uri);
                    ContentResolver cr = this.getContentResolver();
                    Cursor c = cr.query(uri, null, null, null, null);
                    c.moveToFirst();
                    //這是獲取的圖片保存在sdcard中的位置
                    srcPath = c.getString(c.getColumnIndex("_data"));
                    System.out.println(srcPath+"----------保存路徑2");
                    break;
                default:
                    break;
            };
        }
//    	n =1;
    }


    private void submitUploadFile(){
    	final File file=new File(srcPath);
    	final String RequestURL=URLs.UPLOADPHOTO;
    	if (file == null || (!file.exists())) {
    		return;
    	}

		Log.i(TAG, "請求的URL=" + RequestURL);
		Log.i(TAG, "請求的fileName=" + file.getName());
		final Map<String, String> params = new HashMap<String, String>();  
        params.put("user_id", loginKey);  
        params.put("file_type", "1");  
        params.put("content", img_content.getText().toString());  
        showProgressDialog();
    	new Thread(new Runnable() { //開啓線程上傳文件
    		@Override
    		public void run() {
    			uploadFile(file, RequestURL,params);
    		}
    	}).start();
    }

    /**
     * android上傳文件到服務器
     * @param file  需要上傳的文件
     * @param RequestURL  請求的rul
     * @return  返回響應的內容
     */
    private String uploadFile(File file,String RequestURL,Map<String, String> param){
        String result = null;
        String  BOUNDARY =  UUID.randomUUID().toString();  //邊界標識   隨機生成
        String PREFIX = "--" , LINE_END = "\r\n";
        String CONTENT_TYPE = "multipart/form-data";   //內容類型
        // 顯示進度框
//		showProgressDialog();
        try {
            URL url = new URL(RequestURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(TIME_OUT);
            conn.setConnectTimeout(TIME_OUT);
            conn.setDoInput(true);  //允許輸入流
            conn.setDoOutput(true); //允許輸出流
            conn.setUseCaches(false);  //不允許使用緩存
            conn.setRequestMethod("POST");  //請求方式
            conn.setRequestProperty("Charset", CHARSET);  //設置編碼
            conn.setRequestProperty("connection", "keep-alive");
            conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY);
            if(file!=null){
                /**
                 * 當文件不爲空,把文件包裝並且上傳
                 */
                DataOutputStream dos = new DataOutputStream( conn.getOutputStream());
                StringBuffer sb = new StringBuffer();
                
                String params = "";  
                if (param != null && param.size() > 0) {  
                    Iterator<String> it = param.keySet().iterator();  
                    while (it.hasNext()) {  
                        sb = null;  
                        sb = new StringBuffer();  
                        String key = it.next();  
                        String value = param.get(key);  
                        sb.append(PREFIX).append(BOUNDARY).append(LINE_END);  
                        sb.append("Content-Disposition: form-data; name=\"").append(key).append("\"").append(LINE_END).append(LINE_END);  
                        sb.append(value).append(LINE_END);  
                        params = sb.toString();  
                        Log.i(TAG, key+"="+params+"##");  
                        dos.write(params.getBytes());  
//                      dos.flush();  
                    }  
                }  
                sb = new StringBuffer();
                sb.append(PREFIX);
                sb.append(BOUNDARY);
                sb.append(LINE_END);
                /**
                 * 這裏重點注意:
                 * name裏面的值爲服務器端需要key   只有這個key 纔可以得到對應的文件
                 * filename是文件的名字,包含後綴名的   比如:abc.png
                 */
                sb.append("Content-Disposition: form-data; name=\"upfile\";filename=\""+file.getName()+"\""+LINE_END);
                sb.append("Content-Type: image/pjpeg; charset="+CHARSET+LINE_END);
                sb.append(LINE_END);
                dos.write(sb.toString().getBytes());
                InputStream is = new FileInputStream(file);
                byte[] bytes = new byte[1024];
                int len = 0;
                while((len=is.read(bytes))!=-1){
                    dos.write(bytes, 0, len);
                }
                is.close();
                dos.write(LINE_END.getBytes());
                byte[] end_data = (PREFIX+BOUNDARY+PREFIX+LINE_END).getBytes();
                dos.write(end_data);
                
                dos.flush();
                /**
                 * 獲取響應碼  200=成功
                 * 當響應成功,獲取響應的流
                 */

                int res = conn.getResponseCode();
                System.out.println("res========="+res);
                if(res==200){
                    InputStream input =  conn.getInputStream();
                    StringBuffer sb1= new StringBuffer();
                    int ss ;
                    while((ss=input.read())!=-1){
                        sb1.append((char)ss);
                    }
                    result = sb1.toString();
//                 // 移除進度框
//    				removeProgressDialog();
                    finish();
                }
                else{
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


}

xml代碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/photo_full"
    android:orientation="vertical" >
    
    <ImageView
        android:id="@+id/img"
        android:layout_width="fill_parent"
        android:layout_height="350dp"
        android:layout_weight="0.6" />

    <EditText
        android:id="@+id/img_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:ems="10"
        android:inputType="textMultiLine"
        android:lines="3" >
    </EditText>
    
    <Button
        android:id="@+id/natives"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:text="本地選擇" />

    <Button
        android:id="@+id/pai"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:text="拍照上傳" />

    <Button
        android:id="@+id/submit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:text="確定" />

</LinearLayout>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章