Android調取系統相簿和相機照片設定到ImageView並上傳到伺服器
阿新 • • 發佈:2019-01-05
網上的這類答案很多,不同的手機也會出現不同的情況,自己每次做這個功能都要掉進去很多次坑,分享給大家,也自己做一個筆記
話不多先看下效果
程式碼開始:
首先定義一個 PopupWindow
private PopupWindow pop = null;
然後在監聽裡面這麼寫:
pop = new PopupWindow(ModifiedDataActivity.this); View view = getLayoutInflater().inflate(R.layout.item_popupwindows, null);//載入佈局 pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); //設定PopupWindow 一些引數pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); pop.setBackgroundDrawable(new BitmapDrawable()); pop.setFocusable(true); pop.setOutsideTouchable(true); pop.setContentView(view); pop.showAtLocation(view, Gravity.BOTTOM, 0, 0); RelativeLayout parent = (RelativeLayout) view.findViewById(R.id.parent); //最外層佈局 Button bt1 = (Button) view.findViewById(R.id.item_popupwindows_camera); Button bt2 = (Button) view.findViewById(R.id.item_popupwindows_Photo); Button bt3 = (Button) view.findViewById(R.id.item_popupwindows_cancel); parent.setOnClickListener(new View.OnClickListener() { // 設定點選最外層佈局關閉PopupWindow@Override public void onClick(View v) { pop.dismiss(); } }); bt1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {//相機 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//呼叫android自帶的照相機 Uri photoUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; startActivityForResult(intent, 1); pop.dismiss(); } }); bt2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {//相簿 Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//呼叫android的相簿 startActivityForResult(i, 2); pop.dismiss(); } }); bt3.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { pop.dismiss(); } });
//這個方法是拍照或者選擇照片之後系統自動回撥的方法
@Override protected void onActivityResult(int requestCode, int resultCode, final Intent data) { if (resultCode == Activity.RESULT_OK) { if (requestCode == 1) { // 拍照 Bundle extras = data.getExtras(); Bitmap photoBit = (Bitmap) extras.get("data"); Bitmap option = BitmapOption.bitmapOption(photoBit, 5); iv_aprove_head_portrait.setImageBitmap(option); saveBitmap2file(option, "0001.jpg"); final File file = new File("/sdcard/" + "0001.jpg"); Log.e("TAG", "file333333333333333 " + file.getName()); //開始聯網上傳的操作 } else if (requestCode == 2) { // 相簿 try { Uri uri = data.getData(); String[] pojo = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(uri, pojo, null, null, null); if (cursor != null) { ContentResolver cr = this.getContentResolver(); int colunm_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String path = cursor.getString(colunm_index); final File file = new File(path); Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); Bitmap option = BitmapOption.bitmapOption(bitmap, 5); iv_aprove_head_portrait.setImageBitmap(option);//設定為頭像的背景Log.e("TAG", "fiels11111 " + file.getName());
//開始聯網上傳的操作 } } catch (Exception e) { } } } }
//將圖片儲存到本地
static boolean saveBitmap2file(Bitmap bmp, String filename) { Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG; int quality = 100; OutputStream stream = null; try { stream = new FileOutputStream("/sdcard/" + filename); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bmp.compress(format, quality, stream); }
//壓縮圖片的工具類,上面有用到
bitmapOption中的size 代表壓縮的級別
public class BitmapOption { private static final BitmapOption bitmapOption = new BitmapOption(); private BitmapOption() { } public static BitmapOption getBitmapOption() { return bitmapOption; } public static Bitmap bitmapOption(Bitmap image, int size) { ByteArrayOutputStream out = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 85, out); float zoom = (float) Math.sqrt(size * 1024 / (float) out.toByteArray().length); Matrix matrix = new Matrix(); matrix.setScale(zoom, zoom); Bitmap result = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true); out.reset(); result.compress(Bitmap.CompressFormat.JPEG, 85, out); while (out.toByteArray().length > size * 1024) { System.out.println(out.toByteArray().length); matrix.setScale(0.9f, 0.9f); result = Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), matrix, true); out.reset(); result.compress(Bitmap.CompressFormat.JPEG, 85, out); } return result; } }