android设置头像并保存至本地sd卡

 转自https://blog.csdn.net/u012386435/article/details/52275133

非常好用,直接套就可以,自己用了,觉得方便,特意推荐给大家

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import com.igbs_phone.R;
 
public class Personaldata extends Activity {
 
	/**
	 * @param args
	 */
	ImageView image_qq1;
	private Bitmap head;// 头像Bitmap
	@SuppressLint("SdCardPath")
	private static String path = "/sdcard/myHead/";// sd路径
	// private static String path=Environment
	// .getExternalStorageDirectory().getPath()+"/ilex_image";
	Button imageButton, queren_button;
	FragmentTransaction fragmentTransaction;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.personaldata);
		image_qq1 = (ImageView) findViewById(R.id.image_qq1);
		image_qq1.setBackgroundColor(Color.TRANSPARENT);
		imageButton = (Button) findViewById(R.id.re_bt);
		queren_button = (Button) findViewById(R.id.queren_button);
		image_qq1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Intent.ACTION_PICK, null);
				intent.setDataAndType(
						MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
				startActivityForResult(intent, 1);
			}
		});
		imageButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				onBackPressed();
			}
		});
		// 头像转换确定按钮监听
		queren_button.setOnClickListener(new OnClickListener() {
 
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 点击变换头像 确定按钮监听!
 
			}
		});
	}
 
	@Override
	public void onStart() {
		super.onStart();
		Bitmap bt = getBitmap(path + "head.jpg");
		if (bt != null) {
			@SuppressWarnings("deprecation")
			Drawable drawable = new BitmapDrawable(bt);
			image_qq1.setImageDrawable(drawable);
 
		} else {
 
		}
	}
 
	@Override
	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) {
					/**
					 * 上传服务器代码
					 */
 
					// head = toRoundBitmap1(head);//调用圆角处理方法
					setPicToView(head);// 保存在SD卡中
					image_qq1.setImageBitmap(head);// 用ImageView显示出来
					if (head != null && head.isRecycled()) {
						head.recycle();
					}
 
				}
			}
			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();
			}
 
		}
	}
 
	// 从本地的文件中以保存的图片中 获取图片的方法
	private Bitmap getBitmap(String pathString) {
		Bitmap bitmap = null;
		try {
			File file = new File(pathString);
			if (file.exists()) {
				bitmap = BitmapFactory.decodeFile(pathString);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bitmap;
	}
	
}

点击确定保存之后所执行的方法。

Mainactivity.class

// 从本地的文件中以保存的图片中 获取图片的方法
		private Bitmap getBitmap(String pathString) {
			Bitmap bitmap = null;
			try {
				File file = new File(pathString);
				if (file.exists()) {
					bitmap = BitmapFactory.decodeFile(pathString);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return bitmap;
		}
 
	@SuppressLint("SdCardPath")
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		String path1 = "/sdcard/myHead/";// sd路径
		Bitmap bt = getBitmap(path1 + "head.jpg");
		if (bt != null) {
			@SuppressWarnings("deprecation")
			Drawable drawable = new BitmapDrawable(bt);
			image_qq.setImageDrawable(drawable);
		}
	}

猜你喜欢

转载自blog.csdn.net/weixin_42474261/article/details/81237958