アイコンをアップロードし、カメラのアルバムファイルをFileファイルに変換
4857 ワード
1,dialogカメラボタン,アルバムボタン,
// dialog
private void getDialog() {
dialog = new Dialog(this);
//
View inflate = LayoutInflater.from(this).inflate(R.layout.popup_window_button, null);
//
inflate.findViewById(R.id.takePhoto).setOnClickListener(this);
inflate.findViewById(R.id.choosePhoto).setOnClickListener(this);
inflate.findViewById(R.id.btn_cancel).setOnClickListener(this);
// Dialog
dialog.setContentView(inflate);
// Activity
Window dialogWindow = dialog.getWindow();
if (dialogWindow == null) {
return;
}
// Dialog
dialogWindow.setGravity(Gravity.BOTTOM);
//
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.y = 10;// Dialog
//
dialogWindow.setAttributes(lp);
dialog.show();//
}
2、ジャンプカメラ、アルバムのクリックイベント
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.takePhoto:
dialog.dismiss();
File f = new File(imgRoot, new Date().getTime() + ".jpg");
uri = Uri.fromFile(f);
// ACTION_IMAGE_CAPTURE( )
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// (EXTRA_OUTPUT )
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, FLAG_CAMERA_REQUEST);
break;
case R.id.choosePhoto:
dialog.dismiss();
//case R.id.btn_choose_album:
intent = new Intent(Intent.ACTION_PICK);
// image/jpeg image/png image/gif,*
intent.setType("image/*");
startActivityForResult(intent, FFLAG_ALBUM_REQUEST);
break;
Activity Resultメソッドでuriを取得し、fileファイルに変換
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
Uri uri = data.getData();
String[] filePathColumns = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(uri, filePathColumns, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePathColumns[0]);
String imagePath = c.getString(columnIndex);
Uri uri2 = new Uri.Builder()
.scheme(UriUtil.LOCAL_FILE_SCHEME)
.path(imagePath)
.build();
instanll_img_logo.setImageURI(uri2);
// sp ,
SharedPreferences sp = getContext().getSharedPreferences("user", 0);
SharedPreferences.Editor edit = sp.edit();
edit.putString("icon", String.valueOf(uri2));
edit.commit();
c.close();
file = new File(imagePath);
loadImg(file);
// P ,
presenter.upLoad(uid, file);
M層
public Observable upLoad(int uid, File file) {
ILoginBean iLoginBean = RetrofitManager.getInstence().create(ILoginBean.class);
//RequestBody
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
// MultipartBody.Part key RequestBody
MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
return iLoginBean.upLoad(uid, part);
}
P層
@SuppressLint("CheckResult")
public void upLoad(int uid, File file) {
model.upLoad(uid,file)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(UpLoadBean upLoadBean) throws Exception {
if (iView != null){
if (upLoadBean != null& "0".equals(upLoadBean.getCode())){
iView.onSuccess(upLoadBean);
}
}
}
}, new Consumer() {
@Override
public void accept(Throwable throwable) throws Exception {
if (iView != null){
iView.onFailed(throwable);
}
}
});
pレイヤはviewレイヤが結果を返す方法を呼び出す.変換が終了しました.