Android开发 点击在本地选择换图片(头像)并且保存,实例总结。

来源:互联网 发布:马云说的阿里健康 编辑:程序博客网 时间:2024/06/08 08:07

类似于QQ、微信头像。


废话不多说 ,直接上代码


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;@Overrideprotected 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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent(Intent.ACTION_PICK, null);intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");startActivityForResult(intent, 1);}});imageButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubonBackPressed();}});// 头像转换确定按钮监听queren_button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub// 点击变换头像 确定按钮监听!}});}@Overridepublic 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 {}}@Overrideprotected 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")@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.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);}}



0 0
原创粉丝点击