(安卓)图片上传

来源:互联网 发布:便利蜂上班怎么样 知乎 编辑:程序博客网 时间:2024/05/16 19:05



图片的上传至服务器  使用MVP模式:

MainActivity:
import android.content.Intent;import android.graphics.Bitmap;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.Toast;import com.facebook.drawee.backends.pipeline.Fresco;import com.facebook.drawee.view.SimpleDraweeView;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.OnClick;import bwie.com.day_lianxi_1220.bean.MessageBean;import bwie.com.day_lianxi_1220.http.ImageUtils;import bwie.com.day_lianxi_1220.presenter.NewsPresenter;import bwie.com.day_lianxi_1220.view.IView;import okhttp3.MediaType;import okhttp3.MultipartBody;import okhttp3.RequestBody;public class MainActivity extends AppCompatActivity implements IView{    @BindView(R.id.my_image_view)    SimpleDraweeView mMyImageView;    @BindView(R.id.btn_xiangji)    Button mBtnXiangji;    @BindView(R.id.btn_xiangce)    Button mBtnXiangce;    @BindView(R.id.btn_cancel)    Button mBtnCancel;    private File tempFile;    private static final String PHOTO_FILE_NAME = "temp_photo.jpg";    private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照    private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择    private static final int PHOTO_REQUEST_CUT = 3;// 结果    private NewsPresenter presenter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        Fresco.initialize(this);        setContentView(R.layout.activity_main);        ButterKnife.bind(this);        presenter = new NewsPresenter();        presenter.attachView(this);    }    @Override    public void success(MessageBean o) {        String msg = o.getMsg();        Log.e("zxz",msg);    }    @Override    public void falise(Exception e) {    }    @OnClick({R.id.my_image_view, R.id.btn_xiangji, R.id.btn_xiangce, R.id.btn_cancel})    public void onClick(View v) {        switch (v.getId()) {            default:                break;            case R.id.my_image_view:                break;            case R.id.btn_xiangji:                // 激活相机                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");                // 判断存储卡是否可以用,可用进行存储                if (hasSdcard()) {                    tempFile = new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME);                    // 从文件中创建uri                    Uri uri = Uri.fromFile(tempFile);                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);                }                // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CAREMA                startActivityForResult(intent, PHOTO_REQUEST_CAREMA);                break;            case R.id.btn_xiangce:                // 激活系统图库,选择一张图片                Intent intent1 = new Intent(Intent.ACTION_PICK);                intent1.setType("image/*");                // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_GALLERY                startActivityForResult(intent1, PHOTO_REQUEST_GALLERY);                break;            case R.id.btn_cancel:                break;        }    }    /*  * 判断sdcard是否被挂载  */    private boolean hasSdcard() {        //判断SD卡手否是安装好的   media_mounted        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {            return true;        } else {            return false;        }    }    /*    * 剪切图片    */    private void crop(Uri uri) {        // 裁剪图片意图        Intent intent = new Intent("com.android.camera.action.CROP");        intent.setDataAndType(uri, "image/*");        intent.putExtra("crop", "true");        // 裁剪框的比例,1:1        intent.putExtra("aspectX", 1);        intent.putExtra("aspectY", 1);        // 裁剪后输出图片的尺寸大小        intent.putExtra("outputX", 250);        intent.putExtra("outputY", 250);        intent.putExtra("outputFormat", "JPEG");// 图片格式        intent.putExtra("noFaceDetection", true);// 取消人脸识别        intent.putExtra("return-data", true);        // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CUT        startActivityForResult(intent, PHOTO_REQUEST_CUT);    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        if (requestCode == PHOTO_REQUEST_GALLERY) {            // 从相册返回的数据            if (data != null) {                // 得到图片的全路径                Uri uri = data.getData();                crop(uri);            }        } else if (requestCode == PHOTO_REQUEST_CAREMA) {            // 从相机返回的数据            if (hasSdcard()) {                crop(Uri.fromFile(tempFile));            } else {                Toast.makeText(MainActivity.this, "未找到存储卡,无法存储照片!", Toast.LENGTH_SHORT).show();            }        } else if (requestCode == PHOTO_REQUEST_CUT) {            // 从剪切图片返回的数据            if (data != null) {                Bitmap bitmap = data.getParcelableExtra("data");                /**                 * 获得图片                 */                mMyImageView.setImageBitmap(bitmap);                setImgByStr( bitmap);            }            try {                // 将临时文件删除                tempFile.delete();            } catch (Exception e) {                e.printStackTrace();            }        }        super.onActivityResult(requestCode, resultCode, data);    }    /**     * 上传头像     */    public  void setImgByStr(Bitmap bitmap) {        if(bitmap != null){            // 拿着imagePath上传了            // ...        }        String imagePath = ImageUtils.savePhoto(bitmap, Environment                .getExternalStorageDirectory().getAbsolutePath(), String                .valueOf(System.currentTimeMillis()));        Log.d("zxz","imagePath:"+imagePath);        if(imagePath!=null){            File file=new File(String.valueOf(bitmap));//将要保存图片的路径            try {                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);                bos.flush();                bos.close();            } catch (IOException e) {                e.printStackTrace();            }            RequestBody photoRequestBody = RequestBody.create(MediaType.parse("image/png"), file);            MultipartBody.Part photo = MultipartBody.Part.createFormData("file", file.getName(), photoRequestBody);            presenter.getData("71",photo);        }    }}


view层接口:

import bwie.com.day_lianxi_1220.bean.MessageBean; public interface IView {    void success(MessageBean o);    void falise(Exception e);}

p层:

public interface BasePresenter{    void getData(String uid, MultipartBody.Part file);}

import android.util.Log;import bwie.com.day_lianxi_1220.bean.MessageBean;import bwie.com.day_lianxi_1220.model.NewModel;import bwie.com.day_lianxi_1220.view.IView;import io.reactivex.Flowable;import io.reactivex.android.schedulers.AndroidSchedulers;import io.reactivex.schedulers.Schedulers;import io.reactivex.subscribers.DisposableSubscriber;import okhttp3.MultipartBody;/** * Created by 迷人的脚毛!! on 2017/12/20. */public class NewsPresenter implements BasePresenter{    private IView iv;    private DisposableSubscriber<MessageBean> disposableSubscriber;    public void attachView(IView iv){        this.iv = iv;    }    public void detachView(){        // 当Activity销毁的时候取消订阅时间,防止内存泄漏        if (disposableSubscriber != null) {            if (disposableSubscriber.isDisposed()) {                disposableSubscriber.dispose();            }        }        if (iv!=null){            iv = null;        }    }    @Override    public void getData(String uid, MultipartBody.Part file) {        NewModel newModel = new NewModel(this);        newModel.getData(uid,file);    }    public void getNews(Flowable<MessageBean> flowable) {        disposableSubscriber = flowable.subscribeOn(Schedulers.io())                .observeOn(AndroidSchedulers.mainThread())                .subscribeWith(new DisposableSubscriber<MessageBean>() {                    @Override                    public void onNext(MessageBean messageBean) {                        if(messageBean!=null){                            iv.success(messageBean);                        }                    }                    @Override                    public void onError(Throwable t) {                        Log.e("zxz",t.getMessage());                    }                    @Override                    public void onComplete() {                    }                });    }}

model层:

public class NewModel {    private NewsPresenter presenter;    public NewModel(NewsPresenter presenter) {        this.presenter = presenter;    }    public void getData(String uid , MultipartBody.Part file){        Flowable<MessageBean> flowable = RetrofitUtils.getInstance().getService().getMusicList(uid, file);        presenter.getNews(flowable);    }}


工具类:

import io.reactivex.Flowable;import okhttp3.MultipartBody;import retrofit2.http.Multipart;import retrofit2.http.POST;import retrofit2.http.Part;import retrofit2.http.Query; public interface ApiService {    @Multipart    @POST("upload?")    Flowable<MessageBean> getMusicList(@Query("uid") String uid, @Part MultipartBody.Part file);}

import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.graphics.Rect;import android.graphics.RectF;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException; public class ImageUtils {    /**     * Save image to the SD card     *     * @param photoBitmap     * @param photoName     * @param path     */    public static String savePhoto(Bitmap photoBitmap, String path,                                   String photoName) {        String localPath = null;        if (android.os.Environment.getExternalStorageState().equals(                android.os.Environment.MEDIA_MOUNTED)) {            File dir = new File(path);            if (!dir.exists()) {                dir.mkdirs();            }            File photoFile = new File(path, photoName + ".png");            FileOutputStream fileOutputStream = null;            try {                fileOutputStream = new FileOutputStream(photoFile);                if (photoBitmap != null) {                    if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100,                            fileOutputStream)) { // 转换完成                        localPath = photoFile.getPath();                        fileOutputStream.flush();                    }                }            } catch (FileNotFoundException e) {                photoFile.delete();                localPath = null;                e.printStackTrace();            } catch (IOException e) {                photoFile.delete();                localPath = null;                e.printStackTrace();            } finally {                try {                    if (fileOutputStream != null) {                        fileOutputStream.close();                        fileOutputStream = null;                    }                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return localPath;    }    /**     * 转换图片成圆形     *     * @param bitmap 传入Bitmap对象     * @return     */    public static Bitmap toRoundBitmap(Bitmap bitmap) {        int width = bitmap.getWidth();        int height = bitmap.getHeight();        float roundPx;        float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;        if (width <= height) {            roundPx = width / 2;            top = 0;            bottom = width;            left = 0;            right = width;            height = width;            dst_left = 0;            dst_top = 0;            dst_right = width;            dst_bottom = width;        } else {            roundPx = height / 2;            float clip = (width - height) / 2;            left = clip;            right = width - clip;            top = 0;            bottom = height;            width = height;            dst_left = 0;            dst_top = 0;            dst_right = height;            dst_bottom = height;        }        Bitmap output = Bitmap.createBitmap(width,                height, Bitmap.Config.ARGB_8888);        Canvas canvas = new Canvas(output);        final int color = 0xff424242;        final Paint paint = new Paint();        final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom);        final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom);        final RectF rectF = new RectF(dst);        paint.setAntiAlias(true);        canvas.drawARGB(0, 0, 0, 0);        paint.setColor(color);        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));        canvas.drawBitmap(bitmap, src, dst, paint);        return output;    }}

import okhttp3.OkHttpClient;import retrofit2.Retrofit;import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;import retrofit2.converter.gson.GsonConverterFactory; public class RetrofitUtils {    private static volatile RetrofitUtils instance;    private Retrofit retrofit;    private ApiService apiService;    private RetrofitUtils(){        OkHttpClient client = new OkHttpClient();        retrofit = new Retrofit.Builder()                .client(client)                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())                .addConverterFactory(GsonConverterFactory.create())                .baseUrl("http://120.27.23.105/file/")                .build();        apiService = retrofit.create(ApiService.class);    }    public static RetrofitUtils getInstance(){        if (instance==null){            synchronized (RetrofitUtils.class){                if (null==instance){                    instance = new RetrofitUtils();                }            }        }        return instance;    }    public ApiService getService(){        return apiService;    }}


封装类:

public class MessageBean {    /**     * msg : 天呢!文件不能为空     * code : 1     */    private String msg;    private String code;    public String getMsg() {        return msg;    }    public void setMsg(String msg) {        this.msg = msg;    }    public String getCode() {        return code;    }    public void setCode(String code) {        this.code = code;    }}