国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android應用中拍照后獲取照片路徑并上傳的實例分享

2020-01-02 06:59:23
字體:
來源:轉載
供稿:網友

Activity 中的代碼,我只貼出重要的事件部分代碼

public void doPhoto(View view) {   destoryBimap();   String state = Environment.getExternalStorageState();   if (state.equals(Environment.MEDIA_MOUNTED)) {     Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");     startActivityForResult(intent, 1);   } else {     Toast.makeText(MainActivity.this, "沒有SD卡", Toast.LENGTH_LONG).show();   } }  @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {   Uri uri = data.getData();   if (uri != null) {     this.photo = BitmapFactory.decodeFile(uri.getPath());   }   if (this.photo == null) {     Bundle bundle = data.getExtras();     if (bundle != null) {       this.photo = (Bitmap) bundle.get("data");     } else {       Toast.makeText(MainActivity.this, "拍照失敗", Toast.LENGTH_LONG).show();       return;     }   }    FileOutputStream fileOutputStream = null;   try {     // 獲取 SD 卡根目錄     String saveDir = Environment.getExternalStorageDirectory() + "/meitian_photos";     // 新建目錄     File dir = new File(saveDir);     if (! dir.exists()) dir.mkdir();     // 生成文件名     SimpleDateFormat t = new SimpleDateFormat("yyyyMMddssSSS");     String filename = "MT" + (t.format(new Date())) + ".jpg";     // 新建文件     File file = new File(saveDir, filename);     // 打開文件輸出流     fileOutputStream = new FileOutputStream(file);     // 生成圖片文件     this.photo.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);     // 相片的完整路徑     this.picPath = file.getPath();     ImageView imageView = (ImageView) findViewById(R.id.showPhoto);     imageView.setImageBitmap(this.photo);   } catch (Exception e) {     e.printStackTrace();   } finally {     if (fileOutputStream != null) {       try {         fileOutputStream.close();       } catch (Exception e) {         e.printStackTrace();       }     }   } }  /**  * 銷毀圖片文件  */ private void destoryBimap() {   if (photo != null && ! photo.isRecycled()) {     photo.recycle();     photo = null;   } } 

Layout 布局頁面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="fill_parent"   android:layout_height="fill_parent"   android:orientation="vertical"   >   <ScrollView     android:layout_width="fill_parent"     android:layout_height="fill_parent"     >     <LinearLayout       android:layout_width="fill_parent"       android:layout_height="fill_parent"       android:orientation="vertical"       >       <Button         android:id="@+id/doPhoto"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:padding="10dp"         android:layout_marginBottom="10dp"         android:text="拍照"         android:onClick="doPhoto"         />       <TextView         android:id="@+id/showContent"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_marginBottom="10dp"         />       <ImageView         android:id="@+id/showPhoto"         android:layout_width="fill_parent"         android:layout_height="250dp"         android:scaleType="centerCrop"         android:src="@drawable/add"         android:layout_marginBottom="10dp"         />     </LinearLayout>   </ScrollView> </LinearLayout> 

其中的上傳工具類我們下面一起來看:
Android 發送HTTP GET POST 請求以及通過 MultipartEntityBuilder 上傳文件

全部使用新的方式 MultipartEntityBuilder 來處理了。
httpmime-4.3.2.jar   
httpcore-4.3.1.jar  

下載地址:http://hc.apache.org/downloads.cgi
有些鏡像貌似打不開,頁面上可以可以選擇國內的 .cn 后綴的域名鏡像服務器來下載

直接上代碼了:
ZHttpRequset.java

package com.ai9475.util;  import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.protocol.HTTP;  import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset;  /**  * Created by ZHOUZ on 14-2-3.  */ public class ZHttpRequest {   public final String HTTP_GET = "GET";    public final String HTTP_POST = "POST";    /**    * 當前請求的 URL    */   protected String url = "";    /**    * HTTP 請求的類型    */   protected String requsetType = HTTP_GET;    /**    * 連接請求的超時時間    */   protected int connectionTimeout = 5000;    /**    * 讀取遠程數據的超時時間    */   protected int soTimeout = 10000;    /**    * 服務端返回的狀態碼    */   protected int statusCode = -1;    /**    * 當前鏈接的字符編碼    */   protected String charset = HTTP.UTF_8;    /**    * HTTP GET 請求管理器    */   protected HttpRequestBase httpRequest= null;    /**    * HTTP 請求的配置參數    */   protected HttpParams httpParameters= null;    /**    * HTTP 請求響應    */   protected HttpResponse httpResponse= null;    /**    * HTTP 客戶端連接管理器    */   protected HttpClient httpClient= null;    /**    * HTTP POST 方式發送多段數據管理器    */   protected MultipartEntityBuilder multipartEntityBuilder= null;    /**    * 綁定 HTTP 請求的事件監聽器    */   protected OnHttpRequestListener onHttpRequestListener = null;    public ZHttpRequest(){}    public ZHttpRequest(OnHttpRequestListener listener) {     this.setOnHttpRequestListener(listener);   }    /**    * 設置當前請求的鏈接    *    * @param url    * @return    */   public ZHttpRequest setUrl(String url)   {     this.url = url;     return this;   }    /**    * 設置連接超時時間    *    * @param timeout 單位(毫秒),默認 5000    * @return    */   public ZHttpRequest setConnectionTimeout(int timeout)   {     this.connectionTimeout = timeout;     return this;   }    /**    * 設置 socket 讀取超時時間    *    * @param timeout 單位(毫秒),默認 10000    * @return    */   public ZHttpRequest setSoTimeout(int timeout)   {     this.soTimeout = timeout;     return this;   }    /**    * 設置獲取內容的編碼格式    *    * @param charset 默認為 UTF-8    * @return    */   public ZHttpRequest setCharset(String charset)   {     this.charset = charset;     return this;   }    /**    * 獲取當前 HTTP 請求的類型    *    * @return    */   public String getRequestType()   {     return this.requsetType;   }    /**    * 判斷當前是否 HTTP GET 請求    *    * @return    */   public boolean isGet()   {     return this.requsetType == HTTP_GET;   }    /**    * 判斷當前是否 HTTP POST 請求    *    * @return    */   public boolean isPost()   {     return this.requsetType == HTTP_POST;   }    /**    * 獲取 HTTP 請求響應信息    *    * @return    */   public HttpResponse getHttpResponse()   {     return this.httpResponse;   }    /**    * 獲取 HTTP 客戶端連接管理器    *    * @return    */   public HttpClient getHttpClient()   {     return this.httpClient;   }    /**    * 添加一條 HTTP 請求的 header 信息    *    * @param name    * @param value    * @return    */   public ZHttpRequest addHeader(String name, String value)   {     this.httpRequest.addHeader(name, value);     return this;   }    /**    * 獲取 HTTP GET 控制器    *    * @return    */   public HttpGet getHttpGet()   {     return (HttpGet) this.httpRequest;   }    /**    * 獲取 HTTP POST 控制器    *    * @return    */   public HttpPost getHttpPost()   {     return (HttpPost) this.httpRequest;   }    /**    * 獲取請求的狀態碼    *    * @return    */   public int getStatusCode()   {     return this.statusCode;   }    /**    * 通過 GET 方式請求數據    *    * @param url    * @return    * @throws IOException    */   public String get(String url) throws Exception   {     this.requsetType = HTTP_GET;     // 設置當前請求的鏈接     this.setUrl(url);     // 新建 HTTP GET 請求     this.httpRequest = new HttpGet(this.url);     // 執行客戶端請求     this.httpClientExecute();     // 監聽服務端響應事件并返回服務端內容     return this.checkStatus();   }    /**    * 獲取 HTTP POST 多段數據提交管理器    *    * @return    */   public MultipartEntityBuilder getMultipartEntityBuilder()   {     if (this.multipartEntityBuilder == null) {       this.multipartEntityBuilder = MultipartEntityBuilder.create();       // 設置為瀏覽器兼容模式       multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);       // 設置請求的編碼格式       multipartEntityBuilder.setCharset(Charset.forName(this.charset));     }     return this.multipartEntityBuilder;   }    /**    * 配置完要 POST 提交的數據后, 執行該方法生成數據實體等待發送    */   public void buildPostEntity()   {     // 生成 HTTP POST 實體     HttpEntity httpEntity = this.multipartEntityBuilder.build();     this.getHttpPost().setEntity(httpEntity);   }    /**    * 發送 POST 請求    *    * @param url    * @return    * @throws Exception    */   public String post(String url) throws Exception   {     this.requsetType = HTTP_POST;     // 設置當前請求的鏈接     this.setUrl(url);     // 新建 HTTP POST 請求     this.httpRequest = new HttpPost(this.url);     // 執行客戶端請求     this.httpClientExecute();     // 監聽服務端響應事件并返回服務端內容     return this.checkStatus();   }    /**    * 執行 HTTP 請求    *    * @throws Exception    */   protected void httpClientExecute() throws Exception   {     // 配置 HTTP 請求參數     this.httpParameters = new BasicHttpParams();     this.httpParameters.setParameter("charset", this.charset);     // 設置 連接請求超時時間     HttpConnectionParams.setConnectionTimeout(this.httpParameters, this.connectionTimeout);     // 設置 socket 讀取超時時間     HttpConnectionParams.setSoTimeout(this.httpParameters, this.soTimeout);     // 開啟一個客戶端 HTTP 請求     this.httpClient = new DefaultHttpClient(this.httpParameters);     // 啟動 HTTP POST 請求執行前的事件監聽回調操作(如: 自定義提交的數據字段或上傳的文件等)     this.getOnHttpRequestListener().onRequest(this);     // 發送 HTTP 請求并獲取服務端響應狀態     this.httpResponse = this.httpClient.execute(this.httpRequest);     // 獲取請求返回的狀態碼     this.statusCode = this.httpResponse.getStatusLine().getStatusCode();   }    /**    * 讀取服務端返回的輸入流并轉換成字符串返回    *    * @throws Exception    */   public String getInputStream() throws Exception   {     // 接收遠程輸入流     InputStream inStream = this.httpResponse.getEntity().getContent();     // 分段讀取輸入流數據     ByteArrayOutputStream baos = new ByteArrayOutputStream();     byte[] buf = new byte[1024];     int len = -1;     while ((len = inStream.read(buf)) != -1) {       baos.write(buf, 0, len);     }     // 數據接收完畢退出     inStream.close();     // 將數據轉換為字符串保存     return new String(baos.toByteArray(), this.charset);   }    /**    * 關閉連接管理器釋放資源    */   protected void shutdownHttpClient()   {     if (this.httpClient != null && this.httpClient.getConnectionManager() != null) {       this.httpClient.getConnectionManager().shutdown();     }   }    /**    * 監聽服務端響應事件并返回服務端內容    *    * @return    * @throws Exception    */   protected String checkStatus() throws Exception   {     OnHttpRequestListener listener = this.getOnHttpRequestListener();     String content;     if (this.statusCode == HttpStatus.SC_OK) {       // 請求成功, 回調監聽事件       content = listener.onSucceed(this.statusCode, this);     } else {       // 請求失敗或其他, 回調監聽事件       content = listener.onFailed(this.statusCode, this);     }     // 關閉連接管理器釋放資源     this.shutdownHttpClient();     return content;   }    /**    * HTTP 請求操作時的事件監聽接口    */   public interface OnHttpRequestListener   {     /**      * 初始化 HTTP GET 或 POST 請求之前的 header 信息配置 或 其他數據配置等操作      *      * @param request      * @throws Exception      */     public void onRequest(ZHttpRequest request) throws Exception;      /**      * 當 HTTP 請求響應成功時的回調方法      *      * @param statusCode 當前狀態碼      * @param request      * @return 返回請求獲得的字符串內容      * @throws Exception      */     public String onSucceed(int statusCode, ZHttpRequest request) throws Exception;      /**      * 當 HTTP 請求響應失敗時的回調方法      *      * @param statusCode 當前狀態碼      * @param request      * @return 返回請求失敗的提示內容      * @throws Exception      */     public String onFailed(int statusCode, ZHttpRequest request) throws Exception;   }    /**    * 綁定 HTTP 請求的監聽事件    *    * @param listener    * @return    */   public ZHttpRequest setOnHttpRequestListener(OnHttpRequestListener listener)   {     this.onHttpRequestListener = listener;     return this;   }    /**    * 獲取已綁定過的 HTTP 請求監聽事件    *    * @return    */   public OnHttpRequestListener getOnHttpRequestListener()   {     return this.onHttpRequestListener;   } } 

在 Activity 中的使用方法(這里我還是只寫主體部分代碼):
MainActivity.java

public void doClick(View view) {   ZHttpRequest get = new ZHttpRequest();   get       .setCharset(HTTP.UTF_8)       .setConnectionTimeout(5000)       .setSoTimeout(5000);   get.setOnHttpRequestListener(new ZHttpRequest.OnHttpRequestListener() {     @Override     public void onRequest(ZHttpRequest request) throws Exception {      }      @Override     public String onSucceed(int statusCode, ZHttpRequest request) throws Exception {       return request.getInputStream();     }      @Override     public String onFailed(int statusCode, ZHttpRequest request) throws Exception {       return "GET 請求失敗:statusCode "+ statusCode;     }   });    ZHttpRequest post = new ZHttpRequest();   post       .setCharset(HTTP.UTF_8)       .setConnectionTimeout(5000)       .setSoTimeout(10000);   post.setOnHttpRequestListener(new ZHttpRequest.OnHttpRequestListener() {     private String CHARSET = HTTP.UTF_8;     private ContentType TEXT_PLAIN = ContentType.create("text/plain", Charset.forName(CHARSET));      @Override     public void onRequest(ZHttpRequest request) throws Exception {       // 設置發送請求的 header 信息       request.addHeader("cookie", "abc=123;456=愛就是幸福;");       // 配置要 POST 的數據       MultipartEntityBuilder builder = request.getMultipartEntityBuilder();       builder.addTextBody("p1", "abc");       builder.addTextBody("p2", "中文", TEXT_PLAIN);       builder.addTextBody("p3", "abc中文cba", TEXT_PLAIN);       if (picPath != null && ! "".equals(picPath)) {         builder.addTextBody("pic", picPath);         builder.addBinaryBody("file", new File(picPath));       }       request.buildPostEntity();     }      @Override     public String onSucceed(int statusCode, ZHttpRequest request) throws Exception {       return request.getInputStream();     }      @Override     public String onFailed(int statusCode, ZHttpRequest request) throws Exception {       return "POST 請求失敗:statusCode "+ statusCode;     }   });    TextView textView = (TextView) findViewById(R.id.showContent);   String content = "初始內容";   try {     if (view.getId() == R.id.doGet) {       content = get.get("http://www.baidu.com");       content = "GET數據:isGet: " + (get.isGet() ? "yes" : "no") + " =>" + content;     } else {       content = post.post("http://192.168.1.6/test.php");       content = "POST數據:isPost" + (post.isPost() ? "yes" : "no") + " =>" + content;     }    } catch (IOException e) {     content = "IO異常:" + e.getMessage();   } catch (Exception e) {     content = "異常:" + e.getMessage();   }   textView.setText(content); } 

其中 picPath 為 SD 卡中的圖片路徑 String 類型,我是直接拍照后進行上傳用的
布局頁面
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="fill_parent"   android:layout_height="fill_parent"   android:orientation="vertical"   >   <ScrollView     android:layout_width="fill_parent"     android:layout_height="fill_parent"     >     <LinearLayout       android:layout_width="fill_parent"       android:layout_height="fill_parent"       android:orientation="vertical"       >       <Button         android:id="@+id/doGet"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:padding="10dp"         android:layout_marginBottom="10dp"         android:text="GET請求"         android:onClick="doClick"         />       <Button         android:id="@+id/doPost"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:padding="10dp"         android:layout_marginBottom="10dp"         android:text="POST請求"         android:onClick="doClick"         />       <Button         android:id="@+id/doPhoto"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:padding="10dp"         android:layout_marginBottom="10dp"         android:text="拍照"         android:onClick="doPhoto"         />       <TextView         android:id="@+id/showContent"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_marginBottom="10dp"         />       <ImageView         android:id="@+id/showPhoto"         android:layout_width="fill_parent"         android:layout_height="250dp"         android:scaleType="centerCrop"         android:src="@drawable/add"         android:layout_marginBottom="10dp"         />     </LinearLayout>   </ScrollView> </LinearLayout> 

至于服務端我用的 PHP ,只是簡單的輸出獲取到的數據而已

<?php echo 'GET:<br>'. "/n"; //print_r(array_map('urldecode', $_GET)); print_r($_GET); echo '<br>'. "/n". 'POST:<br>'. "/n"; //print_r(array_map('urldecode', $_POST)); print_r($_POST); echo '<br>'. "/n". 'FILES:<br>'. "/n"; print_r($_FILES); echo '<br>'. "/n". 'COOKIES:<br>'. "/n"; print_r($_COOKIE); 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新平| 扎囊县| 长兴县| 蓬溪县| 和硕县| 宜春市| 绩溪县| 资兴市| 安西县| 含山县| 郴州市| 启东市| 昌黎县| 错那县| 贵州省| 湖北省| 汝阳县| 农安县| 尉氏县| 万源市| 沈阳市| 海口市| 中宁县| 乐东| 黑龙江省| 潮安县| 清远市| 宿州市| 方正县| 绥化市| 朝阳区| 清原| 剑阁县| 湖州市| 山丹县| 竹溪县| 宣汉县| 辰溪县| 邹城市| 汨罗市| 定安县|