Android圖片Base64加密+文字上傳
阿新 • • 發佈:2018-12-14
商城專案圖片需要加密上傳,先看下介面:
介面圖如下:
1.兩個介面(加密和上傳介面):
@FormUrlEncoded
@POST("XXX")/**新增商品圖片(base64格式)*/
Flowable<AddGoodsBean> addGoodsImg(@Field("img") String img, @Field("shopid") String shopid, @Field("uid") String uid);
@FormUrlEncoded
@POST("XXX")/**新增商品介面*/
Flowable<CommonBean> addShopping(@Field ("uid") String uid,@Field("shopid") String shopid,@Field("name")String name,
@Field("typeid") String typeid,@Field("count") String count,@Field("cost") String cost,@Field("goods_type") String goods_type,
@Field("img") String img);
2.從相簿選擇圖片,這裡我用的是第三方框架 PhotoPicker,注意許可權別忘記加了
iv_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhotoPicker.builder()
.setPhotoCount(1)
.setShowCamera(false)
.setShowGif(false)
.setPreviewEnabled(false )
.start(TemporaryDishActivity.this, PhotoPicker.REQUEST_CODE);
}
});
3.由於是單張圖片,所以得到的集合直接取第一個就行
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
if (data != null) {
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200,
TemporaryDishActivity.this.getResources().getDisplayMetrics());
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200f,
TemporaryDishActivity.this.getResources().getDisplayMetrics());
ArrayList<String> photos =
data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
img = photos.get(0);
//base64加密圖片上傳
String s = ImageUtils.bitmapToString(img);
mPresenter.addGoodImg(s, String.valueOf(shopid), String.valueOf(uid));
Log.d("lwp", "s:" + s.toString() + " ;img:" + img.toString());
iv_add.setImageBitmap(BitmapFactory.decodeFile(img));
}
}
}
4.工具類:
public class ImageUtils {
// 根據路徑獲得圖片並壓縮,返回bitmap用於顯示
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
//計算圖片的縮放值
public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
//把bitmap轉換成String
public static String bitmapToString(String filePath) {
Bitmap bm = getSmallBitmap(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//1.5M的壓縮後在100Kb以內,測試得值,壓縮後的大小=94486位元組,壓縮後的大小=74473位元組
//這裡的JPEG 如果換成PNG,那麼壓縮的就有600kB這樣
bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);
byte[] b = baos.toByteArray();
Log.d("d", "壓縮後的大小=" + b.length);
return Base64.encodeToString(b, Base64.DEFAULT);
}
}
5.最後上傳成功後結束當前頁面,並且傳送一個Eventbus黏性事件讓之前的頁面重新整理資料
@Override
public void onAddGoodsSuccess() {
// showTip("上傳成功");
EventBus.getDefault().postSticky(new AddTemoporaryDishEvent(true));
finish();
}
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
public void onAddEvent(AddTemoporaryDishEvent event){
if (event.getAdd()){
mPresenter = new ProductsPresenter(this);
mPresenter.get_goodstype(String.valueOf(MyApplication.getInstance().shopid));
showLoadingDialog();
}
}
下面的程式碼和主題就扯遠了.