android 實(shí)現(xiàn)APP中改變頭像圖片的實(shí)例代碼
具體代碼如下所示:
package com.example.studyapplication.fragment;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
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.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.studyapplication.MainActivity;
import com.example.studyapplication.R;
import com.example.studyapplication.wode_Activity;
import com.example.studyapplication.zhubanfangActivity;
import com.example.studyapplication.zhuceActivity;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
import static android.app.Activity.RESULT_OK;
public class WodeFragment extends Fragment implements View.OnClickListener {
private View mView;
private Context mAvtivity;
Button youhuiquan;
Button shoucang;
Button guanzhu;
ImageView shezhi;
ImageView xiaoxi;
TextView qiehuanweizhuban;
static TextView denglu;
LinearLayout daifukuan;
LinearLayout daicanyu;
LinearLayout tuikuan;
LinearLayout yiwancheng;
LinearLayout qingxiandenglu;
LinearLayout log;
LinearLayout quanbudingdan;
private Button LL01,LL02,LL03;
private Context mContext;
private ImageButton touxiang;
private Bitmap head;// 頭像Bitmap
private static String path = "/sdcard/myHead/";// sd路徑
Activity mActivity;
Uri uritempFile;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.wode_layout, null);
mActivity=this.getActivity();
//設(shè)置布局文件5888
return mView;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mContext = getActivity();
}
/*
點(diǎn)擊頭像進(jìn)行更換頭像
*/
touxiang.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.touxiang:// 更換頭像
showTypeDialog();
break;
}
}
});
}
private void initView() {
LL01=(Button)getActivity().findViewById(R.id.youhuiquan);
LL02=(Button) getActivity().findViewById(R.id.shoucang);
LL03=(Button)getActivity().findViewById(R.id.guanzhu);
touxiang = (ImageButton)getActivity().findViewById(R.id.touxiang);
Bitmap bt = BitmapFactory.decodeFile(path + "log.png");// 從SD卡中找頭像,轉(zhuǎn)換成Bitmap
if (bt != null) {
@SuppressWarnings("deprecation")
Drawable drawable = new BitmapDrawable(bt);// 轉(zhuǎn)換成drawable
touxiang.setImageDrawable(drawable);
} else {
/**
* 如果SD里面沒有則需要從服務(wù)器取頭像,取回來的頭像再保存在SD中
*
*/
}
}
private void showTypeDialog() {
//顯示對話框
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final AlertDialog dialog = builder.create();
View view = View.inflate(getActivity(), R.layout.dialog_select_photo, null);
TextView tv_select_gallery = (TextView) view.findViewById(R.id.tv_select_gallery);
TextView tv_select_camera = (TextView) view.findViewById(R.id.tv_select_camera);
tv_select_gallery.setOnClickListener(new View.OnClickListener() {// 在相冊中選取
@Override
public void onClick(View v) {
Intent intent1 = new Intent(Intent.ACTION_PICK, null);
//打開文件
intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/log.png");
startActivityForResult(intent1, 1);
dialog.dismiss();
}
});
tv_select_camera.setOnClickListener(new View.OnClickListener() {// 調(diào)用照相機(jī)
@Override
public void onClick(View v) {
Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent2.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "log.png")));
startActivityForResult(intent2, 2);// 采用ForResult打開
dialog.dismiss();
}
});
dialog.setView(view);
dialog.show();
}
@Override
public 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) {
Uri extras = data.getData();
//head = extras.getParcelable("data");
try {
head=BitmapFactory.decodeStream(mActivity.getContentResolver().openInputStream(uritempFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (head != null) {
/**
* 上傳服務(wù)器代碼
*/
// 讓剛才選擇裁剪得到的圖片顯示在界面上
setPicToView(head);// 保存在SD卡中
touxiang.setImageBitmap(head);// 用ImageButton顯示出來
}
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 調(diào)用系統(tǒng)的裁剪功能
*
* @param uri
*/
public void cropPhoto(Uri uri) {
if(uri== null){
Log.i("tag","The uri is not exist.");
}
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*.png");
//設(shè)置裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是寬高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪圖片寬高
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
intent.putExtra("return-data", true);
uritempFile = Uri.parse("file:///sdcard/temp.jpg");//重要,android4.0以上,本地地址前都加file://+/
// uritempFile=uri;
intent.putExtra(MediaStore.EXTRA_OUTPUT, uritempFile);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
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();// 創(chuàng)建文件夾
String fileName = path + "log.png";// 圖片名字
try {
b = new FileOutputStream(fileName);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把數(shù)據(jù)寫入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
// 關(guān)閉流
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//生成圓角圖片-------未使用
private Bitmap getRoundedCornerBitmap(Bitmap bitmap){
Bitmap roundBitMap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas =new Canvas(roundBitMap);
int color=0xff424242;
Paint paint=new Paint();
//設(shè)置圓形半徑
int radius;
if( bitmap.getWidth()>bitmap.getHeight()){
radius=bitmap.getHeight()/2;
}else {
radius=bitmap.getWidth()/2;
}
//繪制圓形
paint.setAntiAlias(true);
canvas.drawARGB(0,0,0,0);
paint.setColor(color);
canvas.drawCircle(bitmap.getWidth()/2,bitmap.getHeight()/2,radius,paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap,0,0,paint);
return roundBitMap;
}
}
注意:手機(jī)權(quán)限一定要設(shè)置,這個非常重要

AndroidManifest.xml:
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" tols:ignore="ProtectedPermissions" />
總結(jié)
以上所述是小編給大家介紹的android 實(shí)現(xiàn)APP中改變頭像圖片的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
相關(guān)文章
基于android樣式與主題(style&theme)的詳解
本篇文章是對android中的樣式與主題(style&theme)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
RxJava+Retrofit+OkHttp實(shí)現(xiàn)多文件下載之?dāng)帱c(diǎn)續(xù)傳
本篇文章主要介紹了RxJava+Retrofit+OkHttp實(shí)現(xiàn)多文件下載之?dāng)帱c(diǎn)續(xù)傳,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Android 幾種屏幕間跳轉(zhuǎn)的跳轉(zhuǎn)Intent Bundle
這篇文章主要介紹了Android 幾種屏幕間跳轉(zhuǎn)的跳轉(zhuǎn)Intent Bundle,有需要的朋友可以參考一下2013-12-12
基于Android中Webview使用自定義的javascript進(jìn)行回調(diào)的問題詳解
本篇文章對Android中Webview使用自定義的javascript進(jìn)行回調(diào)的問題進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05
Android實(shí)現(xiàn)加載時提示“正在加載,請稍后”的方法
在現(xiàn)在的很多應(yīng)用中,當(dāng)在加載的時候,如果頁面動態(tài)數(shù)據(jù)較多,會有很長一段時間的空白頁面,如果加上這個頁面正在加載的提示,使得應(yīng)用更加人性化。這篇文章就給大家分享了在 Android實(shí)現(xiàn)加載時提示“正在加載,請稍后”的方法,有需要的朋友們可以參考借鑒。2016-10-10
Android應(yīng)用開發(fā)中Fragment的靜態(tài)加載與動態(tài)加載實(shí)例
這篇文章主要介紹了Android應(yīng)用開發(fā)中Fragment的靜態(tài)加載與動態(tài)加載實(shí)例,例子中包括動態(tài)的添加更新以及刪除Fragment等操作,很有借鑒意義,需要的朋友可以參考下2016-02-02
Android 自定義view和屬性動畫實(shí)現(xiàn)充電進(jìn)度條效果
近期項目中需要使用到一種類似手機(jī)電池充電進(jìn)度的動畫效果,以前沒學(xué)屬性動畫的時候,是用圖片+定時器的方式來完成的,下面給大家分享android自定義view和屬性動畫實(shí)現(xiàn)充電進(jìn)度條2016-12-12
Android添加圖片到ListView或者RecyclerView顯示
這篇文章主要介紹了Android添加圖片到ListView或者RecyclerView顯示的相關(guān)資料,需要的朋友可以參考下2016-08-08

