Android用户头像上传
开始写一个小的demo,完成用户圆形头像的选取,在写即将实现的效果之前我们看一下即将要实现的效果图
二,实现代码
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_horizontal"android:orientation="vertical" ><RelativeLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#51CA65"android:padding="30dp" ><ImageView
android:id="@+id/iv_personal_icon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:src="@drawable/default_personal_image" /></RelativeLayout><Button
android:id="@+id/btn_change"android:layout_marginTop="6dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="修改头像" ></Button></LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
MainActivity.java
package com.example.uploadpicdemo;import java.io.File;import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
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.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;public class MainActivity extends Activity {protected static final int CHOOSE_PICTURE = 0;protected static final int TAKE_PICTURE = 1;private static final int CROP_SMALL_PICTURE = 2;protected static Uri tempUri;private ImageView iv_personal_icon;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn_change = (Button) findViewById(R.id.btn_change);iv_personal_icon = (ImageView) findViewById(R.id.iv_personal_icon);btn_change.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {showChoosePicDialog();}});}/*** 显示修改头像的对话框*/protected void showChoosePicDialog() {AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("设置头像");String[] items = { "选择本地照片", "拍照" };builder.setNegativeButton("取消", null);builder.setItems(items, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {switch (which) {case CHOOSE_PICTURE: // 选择本地照片Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);openAlbumIntent.setType("image/*");startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);break;case TAKE_PICTURE: // 拍照Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);tempUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.jpg"));// 指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);startActivityForResult(openCameraIntent, TAKE_PICTURE);break;}}});builder.create().show();}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (resultCode == RESULT_OK) { // 如果返回码是可以用的switch (requestCode) {case TAKE_PICTURE:startPhotoZoom(tempUri); // 开始对图片进行裁剪处理break;case CHOOSE_PICTURE:startPhotoZoom(data.getData()); // 开始对图片进行裁剪处理break;case CROP_SMALL_PICTURE:if (data != null) {setImageToView(data); // 让刚才选择裁剪得到的图片显示在界面上}break;}}}/*** 裁剪图片方法实现* * @param uri*/protected void startPhotoZoom(Uri uri) {if (uri == null) {Log.i("tag", "The uri is not exist.");}tempUri = uri;Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(uri, "image/*");// 设置裁剪intent.putExtra("crop", "true");// aspectX aspectY 是宽高的比例intent.putExtra("aspectX", 1);intent.putExtra("aspectY", 1);// outputX outputY 是裁剪图片宽高intent.putExtra("outputX", 150);intent.putExtra("outputY", 150);intent.putExtra("return-data", true);startActivityForResult(intent, CROP_SMALL_PICTURE);}/*** 保存裁剪之后的图片数据* * @param* * @param picdata*/protected void setImageToView(Intent data) {Bundle extras = data.getExtras();if (extras != null) {Bitmap photo = extras.getParcelable("data");photo = Utils.toRoundBitmap(photo, tempUri); // 这个时候的图片已经被处理成圆形的了iv_personal_icon.setImageBitmap(photo);uploadPic(photo);}}private void uploadPic(Bitmap bitmap) {// 上传至服务器// ... 可以在这里把Bitmap转换成file,然后得到file的url,做文件上传操作// 注意这里得到的图片已经是圆形图片了// bitmap是没有做个圆形处理的,但已经被裁剪了String imagePath = Utils.savePhoto(bitmap, Environment.getExternalStorageDirectory().getAbsolutePath(), String.valueOf(System.currentTimeMillis()));Log.e("imagePath", imagePath+"");if(imagePath != null){// 拿着imagePath上传了// ...}}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
ok,大功告成,最后别忘了在清单文件中添加读写sd可权限,不然得不到imagePath
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Android用户头像上传相关推荐
- Java实现用户头像上传(修改默认文件大小限制)
概述 每次说起文件上传,就不得不提一下前端的实现方式,说来也奇怪,本博主最热门的博客居然也是文件上传,3万多的访问量占了总访问量的一多半:<传统form表单提交方式的文件上传与文件存储>, ...
- PHP+ajaxfileupload 实现用户头像上传
今天写项目的时候需要一个让登录的用户上传头像的功能,然后上网搜了一下,发现有一个不错的Ajax插件ajaxfileupload,所以就拿来用,感觉效果不错,在这里和大家分享一下.下面将用PHP+aja ...
- php文件 用户头像上传代码,网页web上传用户头像代码实现(美图秀秀开放)
网页web上传用户头像代码实现(美图秀秀开放) 在制作论坛或者一些门户社交网站的时候,经常要获取用户的头像.之前我们一般都是自己制作flash插件头像上传.或者用js来自己开发一个头像上传功能.比如有 ...
- Day88.七牛云: 房源图片、用户头像上传 Common-upload、Webuploader
目录 一.七牛云存储 4.鉴权 二.开发者中心,上传.删除测试 1.添加依赖 2.代码测试 3. 封装工具类 三.房源图片上传 1. spring mvc 配置上传支持 2. house/show.h ...
- SpringBoot OSS实战之用户头像上传
文章目录 前言 OSS整合 前端 获取授权 上传图片 上传URL到服务端 完整代码 后端 图片URL接收 补充 效果演示 前言 已经开始对写接口产生厌烦了,毫无技术含量,不过也是最近把用户的比较核心的 ...
- 用户头像上传之 jQuery+ajax+php+预处理
小知识: dataType:预期的服务器返回的数据类型 当设置了dataType:"json"时,后端返回了json,就自动将JSON格式字符串转换为js对象,如果后端返回了Str ...
- android app 头像上传原理
参考:http://www.2cto.com/kf/201401/270167.html 架构:android-async-http 编码:base64,下面是源代码: /** 上传图片*/publi ...
- Android开发之用户头像上传
一,概述 本篇博客总结一下自己在开发过程中应用到的一些知识,在本篇博客中带领大家完成用户头像选择或者拍照上传,并对图片进行大小的压缩,和形状的控制,可以将用户选择到的图片裁剪成圆形上传. ok,我们开 ...
- android自定义头像上传,android裁切图片之用于头像上传
嘛话都不说,直接贴代码,也是在网上找的代码copy出来的! 页面代码 encoding="utf-8"?> android:orientation="vertica ...
最新文章
- 技本功丨请带上纸笔刷着看:解读MySQL执行计划的type列和extra列
- C++类为什么使用private?------封装性
- 陌陌股价过山车背后隐藏了什么?
- 使用SHA1、SHA2双证书进行微软数字签名
- ubuntu 16.04下源码安装opencv3.4
- 基于matlab异步电机 s函数,建立电机状态方程的S 函数和仿真模)基于MATLAB的无刷双馈电机建模与仿真...
- 【JS】//将中文逗号转换为英文逗号
- nfs文件服务器以及客户端基本配置
- Advanced techniques: creating sound, sequencing, timing, scheduling
- 苹果发布无人驾驶研究最新进展,应用机器学习等人工智能热门技术
- 没有足够的值_了解食物的GI值,让你的减脂效率翻倍
- Linux I2C 驱动实验
- 字节飞书前端三轮技术面+HR面
- 直播商城源码,商城开发实现商城底部导航栏
- PK61键盘使用说明
- jQuery属性遍历、HTML操作
- html制作统计期末成绩,如何用Excel制作学生成绩统计表
- 基于Excel的VDS记录数据文件查看及转换工具(转MDA格式)
- 【百金轻】:雄关漫道真如铁,而今迈步从头越。
- 最新突破!天然产物首次实现全合成,轰动整个化学界
热门文章
- 嵌入式系统工程师知识面的宽度、深度、高度
- 超级大数据公司即将诞生 全球招募大数据领域人才
- Leetcode算法题每日一练
- c语言循环中按键跳出,C语言跳出循环
- 取名大师App技术支持
- 本博客搜索,因为csdn的搜索功能不好使,所以使用google做个搜索
- AspNet2.0页面生命周期的各个事件细节
- linux 压缩文件命令
- 关于VUE中对object.object和object[object]的认识
- 埃森哲java开发怎么样_技术丨埃森哲Data Privacy 、商汤科技、平安科技人工智能(AI)类日常实习...