安卓仿微信上傳圖片問題(2)
阿新 • • 發佈:2018-11-10
之前那樣做,發現圖片好模糊,加了一個壓縮圖片的,還有儲存圖片的方法如下:
大神的demo地址:http://blog.csdn.net/jdsjlzx/article/details/44160603#html
微信上傳圖片問題連結:http://blog.csdn.net/lmy0111ly/article/details/50586455#comments
- 照相機以及返回結果
//照相機 public void photo() { File file = new File(Environment.getExternalStorageDirectory(), "/shopping/cache/"); if (!file.exists()) file.mkdirs(); filePath = Environment.getExternalStorageDirectory() + "/shopping/cache/"; fileName = String.valueOf(System.currentTimeMillis() + ".png"); Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath, fileName))); startActivityForResult(openCameraIntent, TAKE_PICTURE); } //照相機返回的圖片 protected void onActivityResult(int requestCode, int resultCode, final Intent data) { switch (requestCode) { case TAKE_PICTURE: if (Bimp.tempSelectBitmap.size() < 9 && resultCode == RESULT_OK) { builder = new CustomDialogLoading.Builder(this, "正在處理圖片,請稍後..."); mDialog = builder.create(); mDialog.show(); Thread thread = new Thread(new Runnable() { @Override public void run() { path = filePath + fileName; Bitmap bm = FileUtils.convertToBitmap(path, 480, 800);//大小壓縮 String savePath = FileUtils.saveBitmap(bm, fileName);//質量壓縮並存儲 ImageItem takePhoto = new ImageItem(); takePhoto.setBitmap(bm); takePhoto.setImagePath(savePath); Bimp.tempSelectBitmap.add(takePhoto); handler.sendEmptyMessage(0); } }); thread.start(); } break; } }
- 壓縮圖片及儲存圖片方法
public class FileUtils { /** * 根據路徑載入bitmap 將縮放後的bitmap返回去 * * @param path 路徑 * @param w 寬 * @param h 長 * @return */ public static Bitmap convertToBitmap(String path, int w, int h) { try { BitmapFactory.Options opts = new BitmapFactory.Options(); // 設定為ture只獲取圖片大小 opts.inJustDecodeBounds = true; opts.inPreferredConfig = Bitmap.Config.ARGB_8888; // 返回為空 BitmapFactory.decodeFile(path, opts); int width = opts.outWidth; int height = opts.outHeight; float scaleWidth = 0.f, scaleHeight = 0.f; if (width > w || height > h) {//如果寬度大於 傳入的寬度 或者 高度大於 傳入的高度大於 // 縮放 scaleWidth = ((float) width) / w; scaleHeight = ((float) height) / h; } opts.inJustDecodeBounds = false; //縮放後的高度和寬度取最大值 float scale = Math.max(scaleWidth, scaleHeight); opts.inSampleSize = (int) scale;//此處是最後的寬高值 // //WeakReference 弱引用 // WeakReference<Bitmap> weak = new WeakReference<Bitmap>actory.decodeFile(path, opts)); // Bitmap bMapRotate = Bitmap.createBitmap(weak.get(BitmapF(), 0, 0, weak.get().getWidth(), weak.get().getHeight(), null, true); Bitmap bMapRotate = BitmapFactory.decodeFile(path, opts); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bMapRotate.compress(Bitmap.CompressFormat.JPEG, 50, baos); ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray()); Bitmap bitmap = BitmapFactory.decodeStream(bais); System.out.println("質量 壓縮---------------width-" + bMapRotate.getWidth() + "---height--" + bMapRotate.getHeight()+"------------------size---"+bMapRotate.getRowBytes()); if (bMapRotate != null) { return bMapRotate; } return null; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 儲存圖片,返回路徑 * @param bm * @param picName * @return */ public static String saveBitmap(Bitmap bm, String picName) { try { if (!isFileExist(picName)) { File tempf = createSDDir(""); } File f = new File(SDPATH, picName); if (f.exists()) { f.delete(); } FileOutputStream out = new FileOutputStream(f); bm.compress(Bitmap.CompressFormat.PNG, 70, out); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return SDPATH + picName; } }