android中以檔案形式上傳圖片到後臺
阿新 • • 發佈:2019-02-10
private File tuPian1 ;
private File tuPian2 ;
@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { String sdStatus = Environment.getExternalStorageState(); if (resultCode == Activity.RESULT_OK) { if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { Log.i("記憶體卡錯誤", "請檢查您的記憶體卡"); } else { switch (requestCode) { case 1: tuPian1= ImageProcessingUtil.selectImage(AuthorizeActivity.this, intent, 80); iv_authorize_front.setImageBitmap(BitmapFactory.decodeFile(tuPian1.getPath())); break; case 2: tuPian2= ImageProcessingUtil.selectImage(AuthorizeActivity.this, intent, 80); iv_authorize_contrary.setImageBitmap(BitmapFactory.decodeFile(tuPian2.getPath())); break; default: return; } } } }
RequestParams params = new RequestParams(); params.put("token", token); params.put("true_name", true_name); params.put("id_no", id_no); try { params.put("id_no_pic", tuPian1); params.put("id_no_pic_z", tuPian2); } catch (FileNotFoundException e) { e.printStackTrace(); } Log.e("params",params+""); Api.userUpdatePicture(this, params, new OnRequestDataListener() { @Override public void requestSuccess(int code, JSONObject data) { toast("認證成功"); } @Override public void requestFailure(int code, String msg) { toast(msg); } });
ImageProcessingUtil類:
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import android.annotation.SuppressLint; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Environment; import android.util.Log; public class ImageProcessingUtil { /** 儲存總目錄 */ public static final String PATH = Environment.getExternalStorageDirectory().getPath() + "/zbm/"; /** 圖片儲存目錄 */ public static final String PICTURE_PATH = PATH + "picture/"; /** 選擇本地圖片 */ public static final String BD = PICTURE_PATH + "bd/"; /** 建立儲存檔案 */ @SuppressLint("SimpleDateFormat") public static File getAlbumDir(String path) { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss"); String timeStamp = format.format(new Date()); String imageFileName = "img_" + timeStamp + ".jpg"; File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } File image = new File(dir, imageFileName); return image; } public static void save(String mCurrentPhotoPath, int siz) { if (mCurrentPhotoPath != null) { try { Bitmap bm = getSmallBitmap(mCurrentPhotoPath); FileOutputStream fos = new FileOutputStream(mCurrentPhotoPath); bm.compress(Bitmap.CompressFormat.JPEG, siz, fos); if (bm != null && !bm.isRecycled()) { bm.recycle(); } } catch (Exception e) { Log.e("圖片壓縮處理.............", "error", e); } } } /** 根據路徑獲得突破並壓縮返回bitmap用於顯示 */ public static Bitmap getSmallBitmap(String filePath) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); int dx = calculateInSampleSize(options, 600, 600); options.inSampleSize = dx; options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); } /** 計算圖片的縮放值 */ public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { int height = options.outHeight; int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { int heightRatio = Math.round((float) height / (float) reqHeight); int widthRatio = Math.round((float) width / (float) reqWidth); if (widthRatio > heightRatio) { inSampleSize = widthRatio; } else { inSampleSize = heightRatio; } } return inSampleSize; } /** 獲取相簿圖片路徑 */ public static File selectImage(Context context, Intent data, int siz) { /* 儲存到本地進行壓縮 */ File fileName = getAlbumDir(BD); Log.e("圖片BD",BD); String path = null; Bitmap bitmap = null; Uri uri = data.getData(); ContentResolver cr = context.getContentResolver(); try { InputStream input = cr.openInputStream(uri); bitmap = BitmapFactory.decodeStream(input); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } FileOutputStream b = null; try { b = new FileOutputStream(fileName); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b); path = fileName.getPath(); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } finally { try { b.flush(); b.close(); } catch (IOException e) { e.printStackTrace(); return null; } } int w = bitmap.getWidth(); int h = bitmap.getHeight(); int y = (w > h) ? w : h; if (y > 2300) { save(path, siz); } if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); } return fileName; } }