您的位置:首页 > 移动开发 > Android开发

Android :修改头像并使用Bmob保存(显示为圆形)

2016-05-09 21:49 691 查看
这几天再写一个项目 之前写登陆注册界面的时候都没有涉及到头像和数据库 所以都比较好实现 这次加上了头像的部分 可以实现和网上的第三方数据库的连接 第三方数据库我使用的是Bmob Bmob 主页

很方便 我们先看一下成果







所以这里面其实东西也还挺多的

所以这篇博客会从这几个方面去介绍如何完成的

1 Bmob的基础配置和注册登录的使用

2 点击修改Button可以出现相册 并且选择一个之后再显示到原来头像位置

3 把头像设置为圆形 选择新头像不论是什么形状都可以显示为圆形

4 上传头像到Bmob

5 觉得底下那两个Button好看的(我觉得挺好看的)嗯那个边框的实现

ok 开始吧:

一 Bmob的基础配置和注册登录的使用

Bmob的主页上面有链接 需要先注册一个账号然后第一次使用先看一下android Bmob快速入门

这个里面主要就是讲的第一次使用时需要配置的还有需要注意的东西

很详细但也需要注意很多地方

我写一下我遇到的一些问题

1.注意as是不需要复制包的 只需要在build里面写代码就可以了

但是千万不要把包导入重复了 比如他的大包里面就包含短信服务那个了 不需要再写了 写了就会出错

2 一定要在配置文件里加上允许联网操作的语句

3 一定要初始化Bmob 就是把密钥放在自己的oncreat( )里面

嗯 这是刚开始的这样子

下来就是要使用Bmob了 Bmob Android开发文档

开发文档很详细 可以仔细看看 我就以我这个项目的注册为例来看一下我是怎么使用的

首先我的用户除了用户名和密码之外还需要邮箱头像电话等信息

所以我继承了BmobUser 写了自己的用户类

注意 用户名和密码是不需要重写的

public class IpetUser extends BmobUser {
private String emile;
private Integer mobile;
private BmobFile image;

public void setEmile(String emile) {
this.emile = emile;
}

public void setMobile(Integer mobile) {
this.mobile = mobile;
}

public String getEmile() {
return emile;
}

public Integer getMobile() {
return mobile;
}

public void setImage(BmobFile image) {
this.image = image;
}

public BmobFile getImage() {
return image;
}
}


头像使用的是BmobFile类型

至于怎么弄的 一会说到头像上传再看

IpetUser user = new IpetUser();


然后通过user.set( )方法可以放入你输入的信息

再通过user.signUp( )注册就好了 挺简单的

这个部分就到这里 如果遇到什么问题可以讨论

二.点击修改Button可以出现相册 并且选择一个之后再显示到原来头像位置

我是使用系统自带相册

在修改按钮里面先是构建了一个intent对象

并将他的action 指定为Intent.ACTION_GET_CONTENT

然后设置必要的参数 通过 startActivityForResult(intent, 1);方法来打开相册程序选择照片了

看一下代码

modify_head.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT这个Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片后返回本画面 */
startActivityForResult(intent, 1);
}
});


所以重点就在
startActivityForResult(intent, 1);
里面

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
path = getImagePath(uri, null);
ContentResolver cr = this.getContentResolver();
try {
Log.e("qwe", path.toString());
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));

/* 将Bitmap设定到ImageView */
res_head.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("qwe", e.getMessage(),e);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private String getImagePath(Uri uri, String seletion) {
String path = null;
Cursor cursor = getContentResolver().query(uri, null, seletion, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
}
cursor.close();

}
return path;

}


其中需要注意的是 path是图片的真实路径 因为在上传的时候需要真实路径

三. 把头像设置为圆形 选择新头像不论是什么形状都可以显示为圆形

这个本来是想自己写的但是在弄的过程中出现了一些问题所以就在git上用了一个

很方便

使用步骤这样的

1.在gradle里面

dependencies {
...
compile 'de.hdodenhof:circleimageview:2.0.0'
}


2 使用在xml里面这样和imageView一样 最后两个属性是可以给图片加边框的

<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/head_image"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@mipmap/cat"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
app:civ_border_width="0.5dp"
app:civ_border_color="#4a9bec" />


在java代码中这样

res_head = (CircleImageView)findViewById(R.id.head_image);


就好啦 准备过一段时间自己实现一个

嗯 到时候一定会实现的

四 上传头像到Bmob

恩恩恩恩恩恩恩恩恩恩恩 我昨天一个晚上都卡死在这里 每册就失败 打印出来原因一直都是filename empty

来看一下步骤、

1 在bmob上添加image列

2 构造BmobFile类型

final BmobFile file=new BmobFile(Susername,null,new File(path).toString());


3 set进去
user.setImage(file);


ok!!!!!!!!!!!!!

但是昨天我问题处在构造类型时只传进去一个参数

所以可以看一下构造方法 最后试了好多遍直到把三个参数都弄进去才注册成功

看一下没有注册之前的数据库



之后



然后那一列也有啦 image那一列只存储了filename 图片在文件里





大概就这样 实现了在Bmob存储图片

五.我觉得我写的Button还挺好看的

<Button
android:id="@+id/registre_button_signup"
android:layout_weight="10"
android:layout_width="0dp"
android:textSize="15dp"
android:hint="Sign up"
android:background="@drawable/sign_button"
android:layout_height="40dp" />


sign_button选择器因为点击是有效果的

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item  android:state_pressed="false"
android:drawable="@drawable/sign_seletor_nopressed"/>
<item  android:state_pressed="true"
android:drawable="@drawable/sign_seletor_pressed"/>
</selector>


sign_seletor_nopressed

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 连框颜色值 --><item>
<shape>
<solid android:color="#17abe3" />
<corners android:radius="20dp" />

</shape>
</item>
<!-- 主体背景颜色值 -->
<item android:bottom="1dp" android:right="1dp" android:top="1dp" android:left="1dp">
<shape>
<gradient android:startColor="#ffffff" android:centerColor="#ffffff"
android:endColor="#ffffff" android:type="linear" android:angle="90"
android:centerX="0.5" android:centerY="0.5" />

<padding android:left="2dp" android:top="2dp" android:right="2dp"
android:bottom="2dp" />
<!-- 应该是圆角 -->
<corners android:radius="20dp" />
</shape>
</item>
</layer-list>


sign_seletor_pressed

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 连框颜色值 --><item>
<shape>
<solid android:color="#17abe3" />
<corners android:radius="20dp" />

</shape>
</item>
<!-- 主体背景颜色值 -->
<item android:bottom="1dp" android:right="1dp" android:top="1dp" android:left="1dp">
<shape>
<gradient android:startColor="#7bbfea" android:centerColor="#7bbfea"
android:endColor="#7bbfea" android:type="linear" android:angle="90"
android:centerX="0.5" android:centerY="0.5" />

<padding android:left="2dp" android:top="2dp" android:right="2dp"
android:bottom="2dp" />
<!-- 应该是圆角 -->
<corners android:radius="20dp" />
</shape>
</item>
</layer-list>


这个部分over.

哦哦哦哦哦哦哦哦哦哦哦哦哦 有点乱

但是下次我就可以写有头像的注册登陆啦!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: