图片上传

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));
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值