图片上传

public class MainActivity extends AppCompatActivity implements View.OnClickListener, MainView {

private ImageView headPhoto;
private TextView tvCamera;
private TextView tvPhoto;
private PopupWindow popupWindow;
public static final String PHOTO_FILE_MAME = "header_image.jpg";//临时文件名
private String path = Environment.getExternalStorageDirectory()+"/header_image.jpg";
public static final int PHOTO_REQUEST_CAMERA = 1;
public static final int PHOTO_REQUEST_PHOTO = 2;
private static final int PHOTO_REQUEST_CUT=3;//裁剪之后
private MainPresenterImpl mainPresenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    headPhoto = (ImageView)findViewById(R.id.head_photo);

    mainPresenter = new MainPresenterImpl();
    mainPresenter.attch(this);

    show();

    headPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popupWindow.showAsDropDown(v);
        }
    });

    tvCamera.setOnClickListener(this);
    tvPhoto.setOnClickListener(this);
}

public void show(){
    View view = View.inflate(MainActivity.this, R.layout.pop_view, null);
    tvCamera = (TextView)view.findViewById(R.id.tv_camera);
    tvPhoto = (TextView)view.findViewById(R.id.tv_photo);

    popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);

    popupWindow.setOutsideTouchable(true);

    popupWindow.setFocusable(true);

    ColorDrawable colorDrawable = new ColorDrawable(getResources().getColor(R.color.colorPrimary));
    popupWindow.setBackgroundDrawable(colorDrawable);

}

//裁剪图片
private void crop(Uri uri){
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    //支持裁剪
    intent.putExtra("CROP",true);
    //裁剪的比例
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    //裁剪后输出图片的尺寸大小
    intent.putExtra("outputX", 250);
    intent.putExtra("outputY", 250);
    //将图片返回给data
    intent.putExtra("return-data",true);
    startActivityForResult(intent, PHOTO_REQUEST_CUT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PHOTO_REQUEST_CAMERA && resultCode == RESULT_OK){
        if (hasSdcard()){
            crop(Uri.fromFile(new File(path)));
        }else {
            Toast.makeText(MainActivity.this, "未找到存储啦,无法存储照片", Toast.LENGTH_SHORT).show();
        }
    }else if (requestCode == PHOTO_REQUEST_PHOTO && resultCode == RESULT_OK){
        //得到图片的全路径
        if (data != null){
            Uri uri = data.getData();
            crop(uri);
        }
    }else if (requestCode == PHOTO_REQUEST_CUT && resultCode == RESULT_OK){
        Bitmap bitmap = data.getParcelableExtra("data");
        Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null, null));

        String[] proj = { MediaStore.Images.Media.DATA };

        Cursor cursor = managedQuery(uri, proj, null, null, null);

        int columnIndexOrThrow = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cursor.moveToFirst();

        String img_path = cursor.getString(columnIndexOrThrow);

        File file = new File(img_path);

        mainPresenter.postImage(Apis.URL, file);
    }

}

//判断SD卡是否挂载
public boolean hasSdcard(){
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
        return true;
    }else {
        return false;
    }
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.tv_camera:
            //打开相机
            Intent intent_takePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (hasSdcard()){//判断sd卡是否可用
                File file = new File(Environment.getExternalStorageDirectory(),PHOTO_FILE_MAME);
                //存储内存
                intent_takePhoto.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
            }
            startActivityForResult(intent_takePhoto, PHOTO_REQUEST_CAMERA);

            popupWindow.dismiss();
            break;
        case R.id.tv_photo:
            Intent intent_choosePhoto = new Intent(Intent.ACTION_PICK);
            intent_choosePhoto.setType("image/*");
            startActivityForResult(intent_choosePhoto, PHOTO_REQUEST_PHOTO);

            popupWindow.dismiss();
            break;
    }
}

@Override
public void success(String data) {

// Log.i(“aaa”, "success: "+data);
PhotoBean photoBean = new Gson().fromJson(data, PhotoBean.class);
String headPath = photoBean.getHeadPath();

    Glide.with(MainActivity.this).load(headPath).into(headPhoto);
}

@Override
public void fail() {

}

}

public interface ObservedApis {

@POST
@Multipart
Observable<ResponseBody> postImage(@Url String url, @Part MultipartBody.Part parts);

}

//工具类中请求方法
public void postImage(String url, File file, ICallBack callBack){
RequestBody requestBody = RequestBody.create(MediaType.parse(“multipart/form-data”), file);
MultipartBody.Part filePart = MultipartBody.Part.createFormData(“image”, file.getName(), requestBody);
mObservedApis.postImage(url, filePart)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserved(callBack));
}

上一篇:Affinity photo Mac已完美支持M1芯片安装 解决M1芯片安装Affinity photo for Mac的问题


下一篇:SpringBoot中对静态资源的放行问题