Android图文上传

这次介绍下如何使用Retrofit+rxjava上传图片和文字

接口的写法
public interface MarketApiService {
    @Multipart//这里一定要加这个
    @POST("market/addNewOrder")
    Observable<ResultBean> addNewOrder(@QueryMap Map<String, String> map, @Part MultipartBody.Part pic);
}
上传的代码
Map<String, String> params = new HashMap<String, String>();
params.put("ship_type_id", type);
params.put("cons_number", consNumber);
params.put("note", beizhu);
File fileByUri = FileUtil.getFileByUri(imagepath, this);
//这里最好写成MediaType.parse("image/*"),之前写成MediaType.parse("image/png")上传失败
RequestBody photoRequestBody = RequestBody.create(MediaType.parse("image/*"), fileByUri);
MultipartBody.Part photo = MultipartBody.Part.createFormData("file", fileByUri.getName(), photoRequestBody);
RetrofitUtils.getInstance().getApiServier(MarketApiService.class)
                    .addNewOrder(params,photo)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<ResultBean>() {
                        @Override
                        public void accept(ResultBean resultBean) throws Exception {

                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {

                        }
                    });
这里的图片选择器使用的是知乎的图片选择器https://github.com/zhihu/Matisse
另外这里还有将Uri转换为File的工具类(因为知乎图片选择器返回的是Uri)
public static File getFileByUri(Uri uri, Context context) {
    String path = null;
    if ("file".equals(uri.getScheme())) {
        path = uri.getEncodedPath();
        if (path != null) {
            path = Uri.decode(path);
            ContentResolver cr = context.getContentResolver();
            StringBuffer buff = new StringBuffer();
            buff.append("(").append(MediaStore.Images.ImageColumns.DATA).append("=").append("'" + path + "'").append(")");
            Cursor cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA }, buff.toString(), null, null);
            int index = 0;
            int dataIdx = 0;
            for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
                index = cur.getColumnIndex(MediaStore.Images.ImageColumns._ID);
                index = cur.getInt(index);
                dataIdx = cur.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                path = cur.getString(dataIdx);
            }
            cur.close();
            if (index == 0) {
            } else {
                Uri u = Uri.parse("content://media/external/images/media/" + index);
                System.out.println("temp uri is :" + u);
            }
        }
        if (path != null) {
            return new File(path);
        }
    } else if ("content".equals(uri.getScheme())) {
        // 4.2.2以后
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            path = cursor.getString(columnIndex);
        }
        cursor.close();

        return new File(path);
    } else {
        //Log.i(TAG, "Uri Scheme:" + uri.getScheme());
    }
    return null;
}
坚持原创技术分享,您的支持将鼓励我继续创作!
-------------本文结束感谢您的阅读-------------