android一个上传图片的样例,包含怎样终止上传过程,假设在上传的时候更新进度条(一)
先上效果图:
Layout为:
- <?
- xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/black"
- android:orientation="vertical">
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:isScrollContainer="true">
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:scaleType="centerInside"
- android:id="@+id/photo_upload_preview_image"
- android:layout_centerInParent="true">
- </ImageView>
- <!-- photo_upload_progress_item的位置跟photo_upload_preview_image是一样的,为了显示photo_upload_preview_image还在loading
- -->
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingTop="10dp"
- android:paddingBottom="10dp"
- android:orientation="horizontal"
- android:id="@+id/photo_upload_progress_item"
- android:layout_centerInParent="true"
- android:visibility="gone">
- <ProgressBar
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginRight="4dp"
- android:layout_gravity="center_vertical"
- android:visibility="visible"
- style="?android:attr/progressBarStyleSmall"
- android:indeterminateDrawable="@drawable/progress_bar_drawable">
- </ProgressBar>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:gravity="center"
- android:textColor="@drawable/white"
- android:text="正在加载照片...">
- </TextView>
- </LinearLayout>
- </RelativeLayout>
- <!-- photo_upload_progress_layout在photo_upload_preview_image的正底下。带有进度条,显示photo_upload_preview_image的上传进度
- -->
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/photo_upload_progress_layout"
- android:orientation="vertical"
- android:background="@drawable/black"
- android:visibility="gone">
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="10dp"
- android:background="@drawable/upload_photo_progress_bg"
- android:layout_marginTop="6dp"
- android:layout_marginBottom="6dp"
- android:layout_marginLeft="6dp"
- android:layout_marginRight="6dp"
- android:layout_gravity="center_horizontal"
- android:paddingLeft="0dp"
- android:paddingRight="0dp"
- android:id="@+id/photo_upload_progress_bar_container">
- <View
- android:layout_width="3dp"
- android:layout_height="fill_parent"
- android:id="@+id/photo_upload_progress_bar"
- android:background="@drawable/upload_photo_progress_bar">
- </View>
- </LinearLayout>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="6dp"
- android:layout_gravity="center_horizontal"
- android:text="照片上传中..."
- android:textColor="@drawable/white">
- </TextView>
- </LinearLayout>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/photo_upload_bottom_bar"
- android:orientation="vertical"
- android:background="#BFBFBF"
- android:paddingLeft="10dp"
- android:paddingRight="10dp"
- android:paddingBottom="4dp"
- android:paddingTop="2dp">
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/photo_upload_album_layout"
- android:paddingTop="5dp"
- android:background="#BFBFBF">
- <TextView
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dp"
- android:text="上传至"
- android:textSize="16dp"
- android:singleLine="true"
- android:textColor="@drawable/gray2"
- android:gravity="center_vertical"
- android:id="@+id/photo_upload_ablum_prompt">
- </TextView>
- <TextView
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/photo_upload_ablum_prompt"
- android:layout_marginLeft="5dp"
- android:text="手机相冊"
- android:textSize="16dp"
- android:singleLine="true"
- android:gravity="center_vertical"
- android:textColor="@drawable/black"
- android:id="@+id/photo_upload_ablum_name">
- </TextView>
- <!-- 在右側,能够选择专辑
- -->
- <Button
- android:id="@+id/photo_upload_choose_album"
- android:layout_width="wrap_content"
- android:layout_height="40dp"
- android:layout_alignParentRight="true"
- android:text="专辑"
- android:textColor="@drawable/black"
- android:paddingRight="30dp"
- android:background="@drawable/location_button">
- </Button>
- </RelativeLayout>
- <!-- 这个在photo_upload_album_layout的以下为位置添加模块
- -->
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/photo_upload_location_layout"
- android:layout_below="@id/photo_upload_album_layout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/photo_upload_location_right_layout"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:background="#BFBFBF"
- android:paddingTop="5dp"
- android:paddingBottom="5dp">
- <Button
- android:id="@+id/photo_upload_add_gps"
- android:layout_width="wrap_content"
- android:layout_height="40dp"
- android:text="位置"
- android:textColor="@drawable/black"
- android:paddingRight="30dp"
- android:background="@drawable/location_button">
- </Button>
- </RelativeLayout>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/photo_upload_location_left_layout"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginRight="5dp"
- android:layout_alignParentLeft="true"
- android:layout_toLeftOf="@id/photo_upload_location_right_layout">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dp"
- android:layout_marginRight="10dp"
- android:id="@+id/photo_upload_location_icon"
- android:src="@drawable/location"
- >
- </ImageView>
- <Button
- android:id="@+id/photo_upload_remove_location"
- android:layout_width="wrap_content"
- android:layout_height="40dp"
- android:text="移除"
- android:textColor="@drawable/black"
- android:background="@drawable/common_button_style"
- android:layout_alignParentRight="true"
- android:layout_marginLeft="5dp"
- android:layout_marginTop="5dp"
- >
- </Button>
- <TextView
- android:id="@+id/photo_upload_location_des"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="@drawable/black"
- android:layout_marginTop="10dp"
- android:layout_toLeftOf="@id/photo_upload_remove_location"
- android:layout_toRightOf="@id/photo_upload_location_icon">
- </TextView>
- </RelativeLayout>
- </RelativeLayout>
- <!-- 这个editText用户能够输入一些信息,在位置模块的正下方
- -->
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/photo_upload_title"
- android:singleLine="true"
- android:maxLength="63"
- android:maxLines="3"
- android:hint="请输入:"
- android:inputType="textMultiLine">
- </EditText>
- <!-- 这两个button在editText的正下方
- -->
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginRight="15dp"
- android:background="@drawable/common_button_style"
- android:text="上传"
- android:id="@+id/photo_upload_button_upload"
- android:textColor="@drawable/black"
- android:layout_weight="1"
- android:textSize="18dp">
- </Button>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/common_button_style"
- android:text="取消"
- android:id="@+id/photo_upload_button_cancel"
- android:textColor="@drawable/black"
- android:layout_weight="1"
- android:textSize="18dp">
- </Button>
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
这里我们是把本地SD根文件夹下的一张图片,转成Bitmap,再存到要上传的tmp文件夹里面:
- private class LoadPhotoTask extends AsyncTask<Void, Void, Boolean>{
- private Bitmap mLoadedBitmap = null;
- protected Boolean doInBackground(Void... params) {
- try {
- if(mFilePathName.length() > 0){
- File file = new File(mFilePathName);
- if (!file.exists()) {
- return false;
- }
- BitmapFactory.Options opt = new BitmapFactory.Options();
- long fileSize = file.length();
- int maxSize = 2*1024 * 1024;
- if(fileSize <= maxSize){
- opt.inSampleSize = 1;
- }else if(fileSize <= maxSize * 4){ <strong>//小于8M</strong>
- opt.inSampleSize = 2;
- }else{
- long times = fileSize / maxSize;
- opt.inSampleSize = (int)(Math.log(times) / Math.log(2.0)) + 1; <strong>//Math.log返回以e为底的对数</strong>
- }
- try{
- mLoadedBitmap = BitmapFactory.decodeFile(mFilePathName,opt);<strong>//opt为缩小的倍数</strong>
- mUploadFilePathName = SaveBitmapToFile(mLoadedBitmap);
- }catch(OutOfMemoryError e){
- Toast.makeText(UploadPhotoActivity.this,
- getResources().getString(R.string.no_memory_to_view_photo),
- Toast.LENGTH_SHORT).show();
- UploadPhotoActivity.this.finish();
- }
- }
- return true;
- } catch (Exception e) {
- Log.e("UploadPhotoActivity", "doInBackground", e);
- return false;
- }
- }
- protected void onPostExecute(Boolean result){
- try {
- showLoadPreviewProgressBar(false);
- if(mLoadedBitmap != null){
- ImageView IamgePreView = (ImageView)findViewById(R.id.photo_upload_preview_image);
- IamgePreView.setImageBitmap(mLoadedBitmap);
- }else{
- }
- mLoadedBitmap = null;
- } catch (Exception e) {
- Log.e("UploadPhotoActivity", "onPostExecute", e);
- }
- }
- }
- private String SaveBitmapToFile(Bitmap bmp){
- if (null == bmp) {
- return null;
- }
- String fileName = "upload_tmp.jpg";
- <strong>File f = this.getFileStreamPath(fileName);</strong>//data/data/com.example.tianqitongtest/files/upload_tmp.jpg,这个是要上传的文件存的位置
- if (f.exists()) {
- f.delete();
- }
- FileOutputStream ostream;
- try {
- <strong>int targetWidth = 780;
- int w = bmp.getWidth();
- if (w > targetWidth) {
- int h = bmp.getHeight();
- int targetHeight = (targetWidth * h) / w;
- bmp = Bitmap.createScaledBitmap(bmp, targetWidth, targetHeight,
- true);//依据指定宽度和高度来生成一个新的Bitmap
- }</strong>
- ostream = this.openFileOutput(fileName, MODE_PRIVATE);
- bmp.compress(Bitmap.CompressFormat.JPEG, 70, ostream);
- ostream.flush();
- ostream.close();
- ostream = null;
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return f.getAbsolutePath();
- }
上传的时候须要把键盘收起:
- private void hideInputMethod(){
- View view = getCurrentFocus();
- if(view != null){
- ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
- }
- }
得到SD卡上的11.jpg的图片路径:
- private String getStoredPicPath() {
- String fileName = "11.jpg";
- return Environment.getExternalStorageDirectory()+"/"+fileName;
- }
android一个上传图片的样例,包含怎样终止上传过程,假设在上传的时候更新进度条(一)的更多相关文章
- JBoss 系列九十六:JBoss MSC - 简介及一个简单演示样例
什么是 JBoss MSC JBoss MSC 即 JBoss Modular Service Container,是第三代 JBoss 产品 JBoss 7和WildFfly的内核,JBoss MS ...
- 使用CEF(二)— 基于VS2019编写一个简单CEF样例
使用CEF(二)- 基于VS2019编写一个简单CEF样例 在这一节中,本人将会在Windows下使用VS2019创建一个空白的C++Windows Desktop Application项目,逐步进 ...
- android AsyncTask异步下载并更新进度条
AsyncTask异步下载并更新进度条 //如果不是很明白请看上篇文章的异步下载 AsyncTask<String, Integer, String> 第一个参数:String 传入 ...
- socket编程——一个简单的样例
从一个简单的使用TCP样例開始socket编程,其基本过程例如以下: server client ++ ...
- 第一个Angular2的样例
欢迎跟我一起学习Angular2 本文根据angular2官网手动敲码得来: 本文地址:http://blog.csdn.net/sushengmiyan 本文作者:苏生米沿 - 开发环境搭建 - 配 ...
- Yii学习笔记之二(使用gii生成一个简单的样例)
1. 数据库准备 (1) 首先我们建一数据库 yii2test 并建立一张表例如以下: DROP TABLE IF EXISTS `posts`; CREATE TABLE `posts` ( `po ...
- Android UI(四)云通讯录项目之云端更新进度条实现
作者:泥沙砖瓦浆木匠网站:http://blog.csdn.net/jeffli1993个人签名:打算起手不凡写出鸿篇巨作的人,往往坚持不了完成第一章节.交流QQ群:[编程之美 365234583]h ...
- 创建android画笔程序的样例(有镜面效果)
先上图: 关键是在检測到手指移动的时候用mPath.quadTo的方法,android sdk解释是: Add a quadratic bezier from the last point, appr ...
- android发送短信样例
Android应用开发中我们经常须要发送手机短信.这对于android平台来说,是最简单只是的功能了,无需太多代码,也无需自己定义代码,仅仅须要调用android提供的消息管理类SmsManager就 ...
随机推荐
- 经验交流List
6月10日:登录功能的实现 第1组,郑超,90 第5组,张琳,90 ----------------------------- 6月11日上午:登录优化 第7组,张朋,90 6月11日下午:查询操作实 ...
- Struts2运行机制(MVC)的分析:
C:(controller)控制器 M:(model)模型处理 V:(view)视图 Struts 2 的运行过程: 核心控制器是FilterDispatcher会过滤 ...
- Android数字签名解析(三)
在刚才開始学习android数字签名的相关知识点的时候,被资料中出现的keystore.x509.密钥对.debug.keystore弄的晕头 转向.经过一段时间的了解,总算明确一些. 一.make_ ...
- EEPlat PaaS VS Saleforce force.com
综述 EEPlatPaaS和Saleforce的Force.com都是元数据驱动应用的解决方式.整体而言,Force.com提供了更上层的解决方式,屏蔽了SQL语句.数据库:EEPlat更加底层,有更 ...
- 文本面板——axure线框图部件库介绍
文本部件用于在页面中显示文字,对于文字的格式可以随意的更改,设定不同的字体.尺寸和颜色. 特别注意:文本面板的高度无法直接调整,它的高度是随着字体的大小自动变化的 在6.5版本中,对文本的排版都有2个 ...
- oracle 的常用语句
第一部分 基本语法 //拼接表字段 select id || 'is' || name from admin select * from emp where ename like '% ...
- 基于visual Studio2013解决C语言竞赛题之1059最大平台
题目 解决代码及点评 /* 功能:编写求最大平台问题的程序. 有n个整数a1,a2, ... an, 若满足a1≤a2≤ ... ≤ an , 则认为在这n个数中有最大平台.在这种情 ...
- jquery特效 幻灯片效果
jquery特效 幻灯片效果,效果图如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Cont ...
- SVM(支持向量机)(一)
(整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) SVM(Support Vector Machines)系列会循序 ...
- Linear Regression(线性回归)(一)—LMS algorithm
(整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 1.问题的引出 先从一个简单的例子说起吧,房地产公司有一些关于Po ...