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

关于android换头像

2016-07-05 09:43 495 查看
最近在在做项目中遇到换头像的问题,简单的介绍一下遇到的问题,首先进入主界面会有一个头像的显示,这个图片是从欢迎界面传过来的值,然后点击主界面头像,进入另一个Activity,是个人信息界面,在这个界面进行头像的更改,你会发现当在个人信息界面修改完图片后,返回主界面并没有更新头像,因为图片是从欢迎界面传来的,主界面不会更新,所以,需要将换的图片保存起来人后在再次调用主界面的时候进行设置。下面是代码操作。

点击主界面头像跳入下一个界面

imageView11.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

Intent intent = new Intent(MainActivity.this, Infopeople.class);

startActivity(intent);

}
});

在Infopeople  Activity 中进行图片更换

imageView11.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);

}
});这里用到了Activityforresult,是图片地址返回,并需要上传到服务器,这里Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,是进入图库进行图片选择
下面是返回图片地址并设置图片,用到了onActivityResult方法

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();

// ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView11.setImageBitmap(BitmapFactory.decodeFile(picturePath));

下面是将得到的图片存入到共用方法中在上传完闭之后将图片地址存入,先贴出存储全局数据的工具类
package com.example.sqgl.util;

import java.util.HashMap;

import android.os.Bundle;

/**
*
* @Title: GlobalParameter.java
* @Package com.ysusoft.utils
* @Description:全局数据
* @author clover
* @date 2014�?1�?�?上午11:15:22
* @version V2.5.6
*/
public class TcGlobalParameter {
public static HashMap<String, Object> gHashmap = new HashMap<String, Object>();

private static Bundle gBundle = new Bundle();

/**
* @param key
* @param value
*/
public static void put(String key, Object value) {
if (gHashmap == null) {
gHashmap = new HashMap<String, Object>();
}
gHashmap.put(key, value);
}

/**
* @param key
* @return
*/
public static Object get(String key) {
if (gHashmap != null) {
return gHashmap.get(key);
}
return null;
}

/**
* 清除指定key的Object
*
* @param key
*/
public static void remove(String key) {
if (gHashmap != null) {
gHashmap.remove(key);
}
}

/**
* 清空Hashamp
*
* @param key
*/
public static void clearAll() {
if (gHashmap != null) {
gHashmap.clear();
}
if (gBundle != null) {
gBundle.clear();
}
}

/**
* Inserts a String value into the mapping of this Bundle, replacing any
* existing value for the given key. Either key or value may be null.
*
* @param key
* a String, or null
* @param value
* a String, or null
*/
public static void putString(String key, String value) {
gBundle.putString(key, value);
}

/**
* Inserts a CharSequence value into the mapping of this Bundle, replacing
* any existing value for the given key. Either key or value may be null.
*
* @param key
* a String, or null
* @param value
* a CharSequence, or null
*/
public static void putCharSequence(String key, CharSequence value) {
gBundle.putCharSequence(key, value);
}

public static String getString(String key) {
return gBundle.getString(key);
}

/**
* 设置Boolean 参数
*
* @param key
* @param value
*/
public static void putboolean(String key, boolean value) {
gBundle.putBoolean(key, value);
}

public static boolean getBoolean(String key) {
return gBundle.getBoolean(key);
}

public static CharSequence getCharSequence(String key) {
return gBundle.getCharSequence(key);
}

public static void putFloat(String key, float value) {
gBundle.putFloat(key, value);
}

public static float getFloat(String key) {
return gBundle.getFloat(key);
}
}
存入图片地址    TcGlobalParameter.put("path", picturePath);
在主界面获取图片在onResume方法,这里注意要记着清空存储的图片。

@Override
protected void onResume() {
// TODO Auto-generated method stub

if ("".equals(TcGlobalParameter.get("path"))
|| null == TcGlobalParameter.get("path")) {

} else {

String path = TcGlobalParameter.get("path").toString();
imageView11.setImageBitmap(BitmapFactory.decodeFile(path));
TcGlobalParameter.remove("path");
}

// getXiaoxidata("1", ID, TcGlobalConstant.Result.yiXiaoxi);
super.onResume();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android