工具类---上传头像
来源:互联网 发布:mac上steam打不开 编辑:程序博客网 时间:2024/04/29 17:13
- -
调用系统相册
protected void gotoXC() { Intent intent=new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 80); intent.putExtra("outputY", 80); intent.putExtra("return-data", true); startActivityForResult(intent, 0); }
调用照相机
protected void gotoXJ() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 1); }
获取图片保存到本地 并显示到imageview
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==Activity.RESULT_OK){
Log.e(“onActivityResult”, resultCode+”“);
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
Log.i(“TestFile”,
“SD card is not avaiable/writeable right now.”);
Log.e(“onActivityResult”, “SD card is not avaiable/writeable right now”);
return;
}
String name = new DateFormat().format("yyyyMMdd_hhmmss",Calendar.getInstance(Locale.CHINA)) + ".jpg"; Bundle bundle = data.getExtras(); Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式 FileOutputStream b = null; File file = new File("/sdcard/myImage/"); file.mkdirs();// 创建文件夹 String fileName = "/sdcard/myImage/"+name; try { b = new FileOutputStream(fileName); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件 } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { b.flush(); b.close(); } catch (IOException e) { e.printStackTrace(); } } Log.e("onActivityResult", "----------------001"); //===调用接口保存头像 成功更换头像 if(true){ Log.e("onActivityResult", "---------------------002"); Drawable drawable =new BitmapDrawable(bitmap); image_avtr.setBackground(null); image_avtr.setImageBitmap(null); image_avtr.setImageDrawable(null); image_avtr.setBackgroundDrawable(null); image_avtr.setBackground(drawable); }else{ ToastUtils.Toas(getApplicationContext(), "上传头像失败,请稍后再试"); }else{ ToastUtils.Toas(getApplicationContext(), "照片获取失败,请稍后再试"); }}
权限
<!-- 调用系统相机权限 --><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
0 0
- 工具类---上传头像
- 快速上传头像到服务端工具类FaceUtil
- 上传头像
- 上传头像
- 上传头像
- 上传头像
- 头像上传
- 上传头像
- 头像上传
- 头像上传
- 头像上传
- 上传头像
- 上传头像
- 头像上传
- 头像上传
- 上传头像
- 头像上传
- 上传头像
- swift2.0基础知识
- 在JSP中调用JAVA类和使用JavaBean有什么区别?
- 【LeetCode】189 Rotate Array 小侃小解
- 设计模式 - 行为型模式 - 备忘录模式
- 经验总结:FPGA时序约束的6种方法
- 工具类---上传头像
- 图像金字塔
- Spring3 MVC 深入研究
- ServletConfig和ServletContext的区别及应用
- 快速对齐你的BarTender条码
- 自写的非常不错的oracle语句 精选
- myeclipse修改代码不加载的问题
- 11/24 初学网页设计
- 数据索引---Solr DIH