关于android换头像

这篇博客主要讨论在Android应用中遇到的头像更新问题。从欢迎界面传递到主界面的头像在个人信息界面修改后无法在主界面显示。解决办法是保存新头像并在重新打开主界面时设置。涉及关键步骤包括使用onActivityResult方法处理图片更换及onResume方法更新头像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在在做项目中遇到换头像的问题,简单的介绍一下遇到的问题,首先进入主界面会有一个头像的显示,这个图片是从欢迎界面传过来的值,然后点击主界面头像,进入另一个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();
	}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值