先上效果图:

Layout为:

  1. <?
  2.  
  3. xml version="1.0" encoding="utf-8"?>
  4. <LinearLayout
  5. xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent"
  8. android:background="@drawable/black"
  9. android:orientation="vertical">
  10.  
  11. <RelativeLayout
  12. xmlns:android="http://schemas.android.com/apk/res/android"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent"
  15. android:layout_weight="1"
  16. android:isScrollContainer="true">
  17. <ImageView
  18. android:layout_width="fill_parent"
  19. android:layout_height="fill_parent"
  20. android:scaleType="centerInside"
  21. android:id="@+id/photo_upload_preview_image"
  22. android:layout_centerInParent="true">
  23. </ImageView>
  24.  
  25. <!-- photo_upload_progress_item的位置跟photo_upload_preview_image是一样的,为了显示photo_upload_preview_image还在loading
  26. -->
  27. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:paddingTop="10dp"
  31. android:paddingBottom="10dp"
  32. android:orientation="horizontal"
  33. android:id="@+id/photo_upload_progress_item"
  34. android:layout_centerInParent="true"
  35. android:visibility="gone">
  36. <ProgressBar
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:layout_marginRight="4dp"
  40. android:layout_gravity="center_vertical"
  41. android:visibility="visible"
  42. style="?android:attr/progressBarStyleSmall"
  43. android:indeterminateDrawable="@drawable/progress_bar_drawable">
  44. </ProgressBar>
  45. <TextView
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:layout_gravity="center_vertical"
  49. android:gravity="center"
  50. android:textColor="@drawable/white"
  51. android:text="正在加载照片...">
  52. </TextView>
  53. </LinearLayout>
  54. </RelativeLayout>
  55.  
  56. <!-- photo_upload_progress_layout在photo_upload_preview_image的正底下。带有进度条,显示photo_upload_preview_image的上传进度
  57. -->
  58. <LinearLayout
  59. xmlns:android="http://schemas.android.com/apk/res/android"
  60. android:layout_width="fill_parent"
  61. android:layout_height="wrap_content"
  62. android:id="@+id/photo_upload_progress_layout"
  63. android:orientation="vertical"
  64. android:background="@drawable/black"
  65. android:visibility="gone">
  66. <LinearLayout
  67. xmlns:android="http://schemas.android.com/apk/res/android"
  68. android:layout_width="fill_parent"
  69. android:layout_height="10dp"
  70. android:background="@drawable/upload_photo_progress_bg"
  71. android:layout_marginTop="6dp"
  72. android:layout_marginBottom="6dp"
  73. android:layout_marginLeft="6dp"
  74. android:layout_marginRight="6dp"
  75. android:layout_gravity="center_horizontal"
  76. android:paddingLeft="0dp"
  77. android:paddingRight="0dp"
  78. android:id="@+id/photo_upload_progress_bar_container">
  79. <View
  80. android:layout_width="3dp"
  81. android:layout_height="fill_parent"
  82. android:id="@+id/photo_upload_progress_bar"
  83. android:background="@drawable/upload_photo_progress_bar">
  84. </View>
  85. </LinearLayout>
  86. <TextView
  87. android:layout_width="wrap_content"
  88. android:layout_height="wrap_content"
  89. android:layout_marginBottom="6dp"
  90. android:layout_gravity="center_horizontal"
  91. android:text="照片上传中..."
  92. android:textColor="@drawable/white">
  93. </TextView>
  94. </LinearLayout>
  95.  
  96. <LinearLayout
  97. xmlns:android="http://schemas.android.com/apk/res/android"
  98. android:layout_width="fill_parent"
  99. android:layout_height="wrap_content"
  100. android:id="@+id/photo_upload_bottom_bar"
  101. android:orientation="vertical"
  102. android:background="#BFBFBF"
  103. android:paddingLeft="10dp"
  104. android:paddingRight="10dp"
  105. android:paddingBottom="4dp"
  106. android:paddingTop="2dp">
  107.  
  108. <RelativeLayout
  109. xmlns:android="http://schemas.android.com/apk/res/android"
  110. android:layout_width="fill_parent"
  111. android:layout_height="wrap_content"
  112. android:id="@+id/photo_upload_album_layout"
  113. android:paddingTop="5dp"
  114. android:background="#BFBFBF">
  115. <TextView
  116. xmlns:android="http://schemas.android.com/apk/res/android"
  117. android:layout_width="wrap_content"
  118. android:layout_height="wrap_content"
  119. android:layout_marginLeft="5dp"
  120. android:text="上传至"
  121. android:textSize="16dp"
  122. android:singleLine="true"
  123. android:textColor="@drawable/gray2"
  124. android:gravity="center_vertical"
  125. android:id="@+id/photo_upload_ablum_prompt">
  126. </TextView>
  127. <TextView
  128. xmlns:android="http://schemas.android.com/apk/res/android"
  129. android:layout_width="wrap_content"
  130. android:layout_height="wrap_content"
  131. android:layout_toRightOf="@id/photo_upload_ablum_prompt"
  132. android:layout_marginLeft="5dp"
  133. android:text="手机相冊"
  134. android:textSize="16dp"
  135. android:singleLine="true"
  136. android:gravity="center_vertical"
  137. android:textColor="@drawable/black"
  138. android:id="@+id/photo_upload_ablum_name">
  139. </TextView>
  140. <!-- 在右側,能够选择专辑
  141. -->
  142. <Button
  143. android:id="@+id/photo_upload_choose_album"
  144. android:layout_width="wrap_content"
  145. android:layout_height="40dp"
  146. android:layout_alignParentRight="true"
  147. android:text="专辑"
  148. android:textColor="@drawable/black"
  149. android:paddingRight="30dp"
  150. android:background="@drawable/location_button">
  151. </Button>
  152. </RelativeLayout>
  153.  
  154. <!-- 这个在photo_upload_album_layout的以下为位置添加模块
  155. -->
  156. <RelativeLayout
  157. xmlns:android="http://schemas.android.com/apk/res/android"
  158. android:id="@+id/photo_upload_location_layout"
  159. android:layout_below="@id/photo_upload_album_layout"
  160. android:layout_width="fill_parent"
  161. android:layout_height="wrap_content">
  162. <RelativeLayout
  163. xmlns:android="http://schemas.android.com/apk/res/android"
  164. android:id="@+id/photo_upload_location_right_layout"
  165. android:layout_width="wrap_content"
  166. android:layout_height="wrap_content"
  167. android:layout_alignParentRight="true"
  168. android:background="#BFBFBF"
  169. android:paddingTop="5dp"
  170. android:paddingBottom="5dp">
  171. <Button
  172. android:id="@+id/photo_upload_add_gps"
  173. android:layout_width="wrap_content"
  174. android:layout_height="40dp"
  175. android:text="位置"
  176. android:textColor="@drawable/black"
  177. android:paddingRight="30dp"
  178. android:background="@drawable/location_button">
  179. </Button>
  180. </RelativeLayout>
  181. <RelativeLayout
  182. xmlns:android="http://schemas.android.com/apk/res/android"
  183. android:id="@+id/photo_upload_location_left_layout"
  184. android:layout_width="wrap_content"
  185. android:layout_height="wrap_content"
  186. android:layout_marginRight="5dp"
  187. android:layout_alignParentLeft="true"
  188. android:layout_toLeftOf="@id/photo_upload_location_right_layout">
  189. <ImageView
  190. android:layout_width="wrap_content"
  191. android:layout_height="wrap_content"
  192. android:layout_marginLeft="5dp"
  193. android:layout_marginRight="10dp"
  194. android:id="@+id/photo_upload_location_icon"
  195. android:src="@drawable/location"
  196. >
  197. </ImageView>
  198.  
  199. <Button
  200. android:id="@+id/photo_upload_remove_location"
  201. android:layout_width="wrap_content"
  202. android:layout_height="40dp"
  203. android:text="移除"
  204. android:textColor="@drawable/black"
  205. android:background="@drawable/common_button_style"
  206. android:layout_alignParentRight="true"
  207. android:layout_marginLeft="5dp"
  208. android:layout_marginTop="5dp"
  209. >
  210. </Button>
  211. <TextView
  212. android:id="@+id/photo_upload_location_des"
  213. android:layout_width="wrap_content"
  214. android:layout_height="wrap_content"
  215. android:textColor="@drawable/black"
  216. android:layout_marginTop="10dp"
  217. android:layout_toLeftOf="@id/photo_upload_remove_location"
  218. android:layout_toRightOf="@id/photo_upload_location_icon">
  219. </TextView>
  220. </RelativeLayout>
  221.  
  222. </RelativeLayout>
  223. <!-- 这个editText用户能够输入一些信息,在位置模块的正下方
  224. -->
  225. <EditText
  226. android:layout_width="fill_parent"
  227. android:layout_height="wrap_content"
  228. android:id="@+id/photo_upload_title"
  229. android:singleLine="true"
  230. android:maxLength="63"
  231. android:maxLines="3"
  232. android:hint="请输入:"
  233. android:inputType="textMultiLine">
  234. </EditText>
  235.  
  236. <!-- 这两个button在editText的正下方
  237. -->
  238.  
  239. <LinearLayout
  240. xmlns:android="http://schemas.android.com/apk/res/android"
  241. android:layout_width="fill_parent"
  242. android:layout_height="wrap_content">
  243. <Button
  244. android:layout_width="wrap_content"
  245. android:layout_height="wrap_content"
  246. android:layout_marginRight="15dp"
  247. android:background="@drawable/common_button_style"
  248. android:text="上传"
  249. android:id="@+id/photo_upload_button_upload"
  250. android:textColor="@drawable/black"
  251. android:layout_weight="1"
  252. android:textSize="18dp">
  253. </Button>
  254. <Button
  255. android:layout_width="wrap_content"
  256. android:layout_height="wrap_content"
  257. android:background="@drawable/common_button_style"
  258. android:text="取消"
  259. android:id="@+id/photo_upload_button_cancel"
  260. android:textColor="@drawable/black"
  261. android:layout_weight="1"
  262. android:textSize="18dp">
  263. </Button>
  264. </LinearLayout>
  265. </LinearLayout>
  266.  
  267. </LinearLayout>

这里我们是把本地SD根文件夹下的一张图片,转成Bitmap,再存到要上传的tmp文件夹里面:

  1. private class LoadPhotoTask extends AsyncTask<Void, Void, Boolean>{
  2. private Bitmap mLoadedBitmap = null;
  3.  
  4. protected Boolean doInBackground(Void... params) {
  5. try {
  6. if(mFilePathName.length() > 0){
  7. File file = new File(mFilePathName);
  8. if (!file.exists()) {
  9. return false;
  10. }
  11. BitmapFactory.Options opt = new BitmapFactory.Options();
  12. long fileSize = file.length();
  13. int maxSize = 2*1024 * 1024;
  14. if(fileSize <= maxSize){
  15. opt.inSampleSize = 1;
  16. }else if(fileSize <= maxSize * 4){ <strong>//小于8M</strong>
  17. opt.inSampleSize = 2;
  18. }else{
  19. long times = fileSize / maxSize;
  20. opt.inSampleSize = (int)(Math.log(times) / Math.log(2.0)) + 1; <strong>//Math.log返回以e为底的对数</strong>
  21. }
  22. try{
  23. mLoadedBitmap = BitmapFactory.decodeFile(mFilePathName,opt);<strong>//opt为缩小的倍数</strong>
  24. mUploadFilePathName = SaveBitmapToFile(mLoadedBitmap);
  25. }catch(OutOfMemoryError e){
  26. Toast.makeText(UploadPhotoActivity.this,
  27. getResources().getString(R.string.no_memory_to_view_photo),
  28. Toast.LENGTH_SHORT).show();
  29. UploadPhotoActivity.this.finish();
  30.  
  31. }
  32. }
  33. return true;
  34. } catch (Exception e) {
  35. Log.e("UploadPhotoActivity", "doInBackground", e);
  36. return false;
  37. }
  38. }
  39.  
  40. protected void onPostExecute(Boolean result){
  41. try {
  42. showLoadPreviewProgressBar(false);
  43. if(mLoadedBitmap != null){
  44. ImageView IamgePreView = (ImageView)findViewById(R.id.photo_upload_preview_image);
  45. IamgePreView.setImageBitmap(mLoadedBitmap);
  46. }else{
  47.  
  48. }
  49. mLoadedBitmap = null;
  50. } catch (Exception e) {
  51. Log.e("UploadPhotoActivity", "onPostExecute", e);
  52. }
  53. }
  54. }
  1. private String SaveBitmapToFile(Bitmap bmp){
  2. if (null == bmp) {
  3. return null;
  4. }
  5. String fileName = "upload_tmp.jpg";
  6. <strong>File f = this.getFileStreamPath(fileName);</strong>//data/data/com.example.tianqitongtest/files/upload_tmp.jpg,这个是要上传的文件存的位置
  7. if (f.exists()) {
  8. f.delete();
  9. }
  10. FileOutputStream ostream;
  11. try {
  12. <strong>int targetWidth = 780;
  13. int w = bmp.getWidth();
  14. if (w > targetWidth) {
  15. int h = bmp.getHeight();
  16. int targetHeight = (targetWidth * h) / w;
  17. bmp = Bitmap.createScaledBitmap(bmp, targetWidth, targetHeight,
  18. true);//依据指定宽度和高度来生成一个新的Bitmap
  19. }</strong>
  20. ostream = this.openFileOutput(fileName, MODE_PRIVATE);
  21. bmp.compress(Bitmap.CompressFormat.JPEG, 70, ostream);
  22. ostream.flush();
  23. ostream.close();
  24. ostream = null;
  25. } catch (Exception e) {
  26. // TODO Auto-generated catch block
  27. e.printStackTrace();
  28. }
  29. return f.getAbsolutePath();
  30. }

上传的时候须要把键盘收起:

  1. private void hideInputMethod(){
  2. View view = getCurrentFocus();
  3. if(view != null){
  4. ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
  5. }
  6. }

得到SD卡上的11.jpg的图片路径:

  1. private String getStoredPicPath() {
  2. String fileName = "11.jpg";
  3. return Environment.getExternalStorageDirectory()+"/"+fileName;
  4. }

android一个上传图片的样例,包含怎样终止上传过程,假设在上传的时候更新进度条(一)的更多相关文章

  1. JBoss 系列九十六:JBoss MSC - 简介及一个简单演示样例

    什么是 JBoss MSC JBoss MSC 即 JBoss Modular Service Container,是第三代 JBoss 产品 JBoss 7和WildFfly的内核,JBoss MS ...

  2. 使用CEF(二)— 基于VS2019编写一个简单CEF样例

    使用CEF(二)- 基于VS2019编写一个简单CEF样例 在这一节中,本人将会在Windows下使用VS2019创建一个空白的C++Windows Desktop Application项目,逐步进 ...

  3. android AsyncTask异步下载并更新进度条

    AsyncTask异步下载并更新进度条    //如果不是很明白请看上篇文章的异步下载 AsyncTask<String, Integer, String> 第一个参数:String 传入 ...

  4. socket编程——一个简单的样例

    从一个简单的使用TCP样例開始socket编程,其基本过程例如以下: server                                                  client ++ ...

  5. 第一个Angular2的样例

    欢迎跟我一起学习Angular2 本文根据angular2官网手动敲码得来: 本文地址:http://blog.csdn.net/sushengmiyan 本文作者:苏生米沿 - 开发环境搭建 - 配 ...

  6. Yii学习笔记之二(使用gii生成一个简单的样例)

    1. 数据库准备 (1) 首先我们建一数据库 yii2test 并建立一张表例如以下: DROP TABLE IF EXISTS `posts`; CREATE TABLE `posts` ( `po ...

  7. Android UI(四)云通讯录项目之云端更新进度条实现

    作者:泥沙砖瓦浆木匠网站:http://blog.csdn.net/jeffli1993个人签名:打算起手不凡写出鸿篇巨作的人,往往坚持不了完成第一章节.交流QQ群:[编程之美 365234583]h ...

  8. 创建android画笔程序的样例(有镜面效果)

    先上图: 关键是在检測到手指移动的时候用mPath.quadTo的方法,android sdk解释是: Add a quadratic bezier from the last point, appr ...

  9. android发送短信样例

    Android应用开发中我们经常须要发送手机短信.这对于android平台来说,是最简单只是的功能了,无需太多代码,也无需自己定义代码,仅仅须要调用android提供的消息管理类SmsManager就 ...

随机推荐

  1. 经验交流List

    6月10日:登录功能的实现 第1组,郑超,90 第5组,张琳,90 ----------------------------- 6月11日上午:登录优化 第7组,张朋,90 6月11日下午:查询操作实 ...

  2. Struts2运行机制(MVC)的分析:

    C:(controller)控制器          M:(model)模型处理    V:(view)视图 Struts 2 的运行过程:     核心控制器是FilterDispatcher会过滤 ...

  3. Android数字签名解析(三)

    在刚才開始学习android数字签名的相关知识点的时候,被资料中出现的keystore.x509.密钥对.debug.keystore弄的晕头 转向.经过一段时间的了解,总算明确一些. 一.make_ ...

  4. EEPlat PaaS VS Saleforce force.com

    综述 EEPlatPaaS和Saleforce的Force.com都是元数据驱动应用的解决方式.整体而言,Force.com提供了更上层的解决方式,屏蔽了SQL语句.数据库:EEPlat更加底层,有更 ...

  5. 文本面板——axure线框图部件库介绍

    文本部件用于在页面中显示文字,对于文字的格式可以随意的更改,设定不同的字体.尺寸和颜色. 特别注意:文本面板的高度无法直接调整,它的高度是随着字体的大小自动变化的 在6.5版本中,对文本的排版都有2个 ...

  6. oracle 的常用语句

    第一部分 基本语法 //拼接表字段 select id   ||   'is'   ||   name from admin select * from emp where ename like '% ...

  7. 基于visual Studio2013解决C语言竞赛题之1059最大平台

       题目 解决代码及点评 /* 功能:编写求最大平台问题的程序. 有n个整数a1,a2, ... an, 若满足a1≤a2≤ ... ≤ an , 则认为在这n个数中有最大平台.在这种情 ...

  8. jquery特效 幻灯片效果

    jquery特效 幻灯片效果,效果图如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Cont ...

  9. SVM(支持向量机)(一)

    (整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) SVM(Support Vector Machines)系列会循序 ...

  10. Linear Regression(线性回归)(一)—LMS algorithm

    (整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 1.问题的引出 先从一个简单的例子说起吧,房地产公司有一些关于Po ...