public class AddCircleActivity extends BaseActivity {
@BindView(R.id.bo_text)
EditText boText;
@BindView(R.id.bo_image_list)
SimpleDraweeView image;
// CirCleAdapter cirCleAdapter;
Bitmap bitmap;
@BindView(R.id.back)
ImageView back;
private int commodityId = 1;
private FabuQuanziBean fabuQuanziBean1;
private HashMap<String, String> hashMap;
private Persion persion;
@Override
protected void bindEvent() {
}
@Override
protected void initData() {
}
@Override
protected void initView() {
ButterKnife.bind(this);
persion = new Persion(this);
// cirCleAdapter = new CirCleAdapter();
// cirCleAdapter.setSign(1);
// cirCleAdapter.add(R.drawable.ic_launcher_background);
// boImageList.setLayoutManager(new GridLayoutManager(this,3));
// boImageList.setAdapter(cirCleAdapter);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String shuru = boText.getText().toString();
File file = BitMap.compressImage(bitmap);
RequestBody requestBody = RequestBody.create(MediaType.parse(“image/*”), file.getAbsoluteFile());
MultipartBody.Part image_part = MultipartBody.Part.createFormData(“image”, file.getName(), requestBody);
hashMap = new HashMap<>();
hashMap.put("commodityId", "" +commodityId);
hashMap.put("content", "" + shuru);
// persion.getfabu(hashMap,file);
Observable getfabu = MyUtil.getutil().api.getfabu(hashMap, image_part);
getfabu.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(FabuQuanziBean fabuQuanziBean) {
fabuQuanziBean1 = fabuQuanziBean;
ToastData(fabuQuanziBean.getMessage()+"");
}
});
}
});
}
@Override
protected int bindLayout() {
return R.layout.activity_add_circle;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: add setContentView(...) invocation
ButterKnife.bind(this);
}
@OnClick(R.id.bo_image_list)
public void onViewClicked() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//获取图片
Uri data1 = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data1);
} catch (IOException e) {
e.printStackTrace();
}
image.setImageBitmap(bitmap);
}
public void getShow(FabuQuanziBean fabuQuanziBean) {
String status = fabuQuanziBean.getStatus();
if (status.equals("0000")){
ToastData(fabuQuanziBean.getMessage());
}
}
}
---------------------------BitMap---------------
public class BitMap {
//用来转换类型的 将Bitmap类型转换为Uri类型
public static File compressImage(Bitmap bitmap) {
ByteArrayOutputStream baos= new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 500) { //循环判断如果压缩后图片是否大于500kb,大于继续压缩
baos.reset();//重置baos即清空baos
options -= 10;//每次都减少10
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
}
//以当前时间命名
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date(System.currentTimeMillis());
//图片名
String filename = format.format(date);
//存储到外存空间
File file = new File(Environment.getExternalStorageDirectory(), filename + ".jpg");
try {
FileOutputStream fos = new FileOutputStream(file);
try {
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return file;
}
}
----------------------------------Api
@Multipart
@POST(“circle/verify/v1/releaseCircle”)
Observable getfabu(@QueryMap HashMap<String, String> params, @Part MultipartBody.Part image);