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

类似于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;

@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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值