Android開發 點選在本地選擇換圖片(頭像)並且儲存,例項總結。
阿新 • • 發佈:2019-01-23
類似於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); } }