Android头像选择
来源:互联网 发布:监听2121端口 编辑:程序博客网 时间:2024/05/19 20:01
简单头像选择
ui设计
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginLeft="33dp"
android:layout_marginTop="106dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
逻辑实现
package com.example.android_name;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView img=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)this.findViewById(R.id.imageView1);
//注册上下文对象
registerForContextMenu(img);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("设置头像");
menu.add(0, 1, 0, "相机中");
menu.add(0, 2, 0, "相册中获取");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
int id=item.getItemId();
switch(id){
case 1:
//创建意图
Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
break;
case 2:
Intent abum=new Intent(Intent.ACTION_GET_CONTENT);
//设置类型
abum.setType("image/*");
startActivityForResult(abum, 2);
break;
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK&&requestCode==1){
//从照相机中获取数据
Bundle budll=data.getExtras();
if(budll!=null){
Bitmap bit= (Bitmap)budll.get("data");
img.setImageBitmap(bit);
}
}else if(resultCode==RESULT_OK&&requestCode==2){
try {
Uri u=data.getData();
ContentResolver con=getContentResolver();
Bitmap b=MediaStore.Images.Media.getBitmap(con, u);
img.setImageBitmap(b);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- android 选择头像上传
- Android头像选择
- Android 选择并裁剪头像
- android头像选择(拍照,相册,裁剪)
- android头像选择(拍照,相册,裁剪)
- Android头像选择(手机和相册)
- Android 仿iOS头像选择效果
- android圆形头像:相机裁剪+相册选择
- 头像选择
- 头像选择
- 头像选择
- Android开发--通过相册或拍照选择头像
- Android入门--实现选择并编辑图片设置成头像
- Android开发--通过相册或拍照选择头像
- Android开发--通过相册或拍照选择头像
- Android开发--通过相册或拍照选择头像
- android学习随记-头像拍照及选择图库
- Android头像的选择并上传到阿里云服务器
- Python3pandas库Series用法(基础整理)
- poi设置excel行高
- PAT甲级 1111. Online Map (30)
- 流水账笔记:PE文件格式(导出表)
- 特征提取与特征选择
- Android头像选择
- docker镜像操作
- PAT 甲级 1112. Stucked Keyboard (20)
- eclipse导入maven项目时报Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources
- Cannot link libpng
- 151. Reverse Words in a String
- 分段多项式及样条估计
- 笔记:数据库操作
- Eclipse 建包时出现 Package already exists. Note: Views might filter empty parent 的解决方案