Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android更換頭像功能實現

Android更換頭像功能實現

編輯:關於Android編程

現在不管什麼APP都有個頭像,如果你看見沒有頭像的APP就會感覺非常奇怪,以前頭像都是方的,後來就變成圓的,我估計在過個幾年就得來個五角星形狀的頭像,下面我把更換頭像的代碼寫來:



    

    

首先我定義了一個非常簡單的XML布局,裡面兩個Button,一個ImageView用來顯示頭像:

下面我來看看MainActivity代碼:

package com.example.changhead;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

@SuppressLint("SdCardPath")
public class MainActivity extends Activity implements OnClickListener {
	private ImageView ivHead;//頭像顯示
	private Button btnTakephoto;//拍照
	private Button btnPhotos;//相冊
	private Bitmap head;//頭像Bitmap
	private static String path="/sdcard/myHead/";//sd路徑

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {
		//初始化控件
		btnPhotos = (Button) findViewById(R.id.btn_photos);
		btnTakephoto = (Button) findViewById(R.id.btn_takephoto);
		btnPhotos.setOnClickListener(this);
		btnTakephoto.setOnClickListener(this);
		ivHead = (ImageView) findViewById(R.id.iv_head);
		Bitmap bt = BitmapFactory.decodeFile(path + "head.jpg");//從Sd中找頭像,轉換成Bitmap
		if(bt!=null){
			@SuppressWarnings("deprecation")
			Drawable drawable = new BitmapDrawable(bt);//轉換成drawable
			ivHead.setImageDrawable(drawable);
		}else{
			/**
			 *	如果SD裡面沒有則需要從服務器取頭像,取回來的頭像再保存在SD中
			 * 
			 */
		}
		
		
	
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_photos://從相冊裡面取照片
			Intent intent1 = new Intent(Intent.ACTION_PICK, null);
			intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
			startActivityForResult(intent1, 1);
			break;
		case R.id.btn_takephoto://調用相機拍照
			Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
							"head.jpg")));
			startActivityForResult(intent2, 2);//采用ForResult打開
			break;
		default:
			break;
		}
	}
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case 1:
			if (resultCode == RESULT_OK) {
				cropPhoto(data.getData());//裁剪圖片
			}

			break;
		case 2:
			if (resultCode == RESULT_OK) {
				File temp = new File(Environment.getExternalStorageDirectory()
						+ "/head.jpg");
				cropPhoto(Uri.fromFile(temp));//裁剪圖片
			}

			break;
		case 3:
			if (data != null) {
				Bundle extras = data.getExtras();
				head = extras.getParcelable("data");
				if(head!=null){
					/**
					 * 上傳服務器代碼
					 */
					setPicToView(head);//保存在SD卡中
					ivHead.setImageBitmap(head);//用ImageView顯示出來
				}
			}
			break;
		default:
			break;

		}
		super.onActivityResult(requestCode, resultCode, data);
	};
	/**
	 * 調用系統的裁剪
	 * @param uri
	 */
	public void cropPhoto(Uri uri) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		intent.putExtra("crop", "true");
		 // aspectX aspectY 是寬高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪圖片寬高
		intent.putExtra("outputX", 150);
		intent.putExtra("outputY", 150);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, 3);
	}
	private void setPicToView(Bitmap mBitmap) {
		 String sdStatus = Environment.getExternalStorageState();  
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 檢測sd是否可用  
               return;  
           }  
		FileOutputStream b = null;
		File file = new File(path);
		file.mkdirs();// 創建文件夾
		String fileName =path + "head.jpg";//圖片名字
		try {
			b = new FileOutputStream(fileName);
			mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把數據寫入文件
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				//關閉流
				b.flush();
				b.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
}
}
上面的注釋已經很詳細了,就這些

對了不要忘記加權限:



  

源碼下載



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved