Android 相机或者相册上传头像


package com.og.common;

import java.io.File;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import com.mk.common.MKActivity;
import com.og.unite.data.OGSdkUser;
import com.og.unite.main.OGIDispatcherCallback;
import com.og.unite.main.OGSdkPlatform;

public class HeadPortraitActivity extends MKActivity {
	
	private static HeadPortraitActivity mInstance;
	
	private static final int PHOTO_REQUEST_CAMERA = 1;	// 拍照
	private static final int PHOTO_REQUEST_GALLERY = 2;	// 从相册中选择
	private static final int PHOTO_REQUEST_CUT = 3;		// 结果
	
	private static final int PHOTO_WH = 120;			// 图片的宽高值
	private ImageView mFace;
	private Bitmap bitmap;
	
	/* 头像名称 */
	private static final String PHOTO_FILE_NAME = "temp_photo.jpg";
	private File tempFile;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d("Head", "HeadPortraitActivity onCreate");
		mInstance = this;
		super.onCreate(savedInstanceState);
	}
	
	public static HeadPortraitActivity getInstance() {
		return mInstance;
	}
	
	public void modifyHeadPortraitByCamera() {
    	MKActivity.getInstance().runOnUiThread(new Runnable() {
			@Override
			public void run() {
				
				Log.d("Head", "HeadPortraitActivity  111");
				Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
				// 判断存储卡是否可以用,可用进行存储
				if (hasSdcard()) {
					intent.putExtra(MediaStore.EXTRA_OUTPUT,
							Uri.fromFile(new File(Environment
									.getExternalStorageDirectory(), PHOTO_FILE_NAME)));
				}
				HeadPortraitActivity.getInstance().startActivityForResult(intent, PHOTO_REQUEST_CAMERA);
				Log.d("Head", "HeadPortraitActivity  222");
			}
    	});
    }
	
	public void modifyHeadPortraitByAlbum() {
		MKActivity.getInstance().runOnUiThread(new Runnable() {

			@Override
			public void run() {
				Log.d("Head", "HeadPortraitActivity  112");
				// 激活系统图库,选择一张图片
				Intent intent = new Intent(Intent.ACTION_PICK);
				intent.setType("image/*");
				HeadPortraitActivity.getInstance().startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
				Log.d("Head", "HeadPortraitActivity  223");
			}
    	});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == PHOTO_REQUEST_GALLERY) {
			if (data != null) {
				// 得到图片的全路径
				Uri uri = data.getData();
				crop(uri);
			}

		} else if (requestCode == PHOTO_REQUEST_CAMERA) {
			if (hasSdcard()) {
				tempFile = new File(Environment.getExternalStorageDirectory(),
						PHOTO_FILE_NAME);
				crop(Uri.fromFile(tempFile));
			} else {
				Toast.makeText(MKActivity.getInstance(), "未找到存储卡,无法存储照片!", 0).show();
			}

		} else if (requestCode == PHOTO_REQUEST_CUT) {
			try {
				bitmap = data.getParcelableExtra("data");
				//this.mFace.setImageBitmap(bitmap);
				
				if(bitmap == null)
				{
					Log.d("Head", "bitmap == null");
				}
				<strong>// 在此将 bitmap 对象传到服务器</strong>
				boolean delete = tempFile.delete();
				System.out.println("delete = " + delete);

			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		super.onActivityResult(requestCode, resultCode, data);
	}
	
	private void crop(Uri uri) {
		// 裁剪图片意图
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		intent.putExtra("crop", "true");
		// 裁剪框的比例,1:1
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// 裁剪后输出图片的尺寸大小
		intent.putExtra("outputX", PHOTO_WH);
		intent.putExtra("outputY", PHOTO_WH);
		// 图片格式
		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra("noFaceDetection", true);	// 取消人脸识别
		intent.putExtra("return-data", true);		// true:不返回uri  false:返回uri
		startActivityForResult(intent, PHOTO_REQUEST_CUT);
	}
	
	private boolean hasSdcard() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			return false;
		}
	}
	
}


AndroidManifest.xml 中添加

<activity 
    	    android:name="com.og.common.HeadPortraitActivity"
    	    android:label="@string/app_name" 
			android:screenOrientation="sensorLandscape" 
			android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
    	    android:configChanges="orientation|keyboard|keyboardHidden|screenSize">
    	</activity>


在主 Activity 的 onCreate 方法中启动上述新建的 Activity

Intent it = new Intent(this, com.og.common.HeadPortraitActivity.class);
		 startActivity(it);



PS:如果该Activity作为主Activity, 则需在上述 <activity  中添加启动内容

<intent-filter>
        		<action android:name="android.intent.action.MAIN" />
        		<category android:name="android.intent.category.LAUNCHER" />
      		</intent-filter>

亲测可行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值