欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

上传头像

程序员文章站 2024-03-25 12:59:34
...
  1. 写上传头像的一个单独的工具类
package com.example.zhangbingfan0506;

import android.graphics.Bitmap;
import android.os.Environment;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BitMapUtils  {
    public static File getFile(Bitmap bitmap)
    {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,50,byteArrayOutputStream);

//        当前时间
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date(System.currentTimeMillis());

        String format1 = format.format(date);
        File file = new File(Environment.getExternalStorageDirectory(), format1 + ".jpg");

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());

           int x=0;
            byte[] bytes = new byte[1024 * 100];
            while ((x=byteArrayInputStream.read(bytes))!=-1)
            {
                fileOutputStream.write(bytes,0,x);
            }
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;

    }
}
  1. 下面的是在你fragment或者是Activity里面写的 可能你MVP里面在你fragment里面实现的V层方法可能吐司不出来 没找到原因 但是他走MVP了
package com.example.zhangbingfan0506;

import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,IbaseView<HeadBean> {

    @BindView(R.id.image1)
    ImageView image1;
    private Unbinder bind;
    private int i;
    private HeadPresenter headPresenter;
    private Dialog dialog;

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

        headPresenter = new HeadPresenter(this);

        image1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show();
            }
        });
    }
    private void show() {
        dialog = new Dialog(this);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog, null);
        view.findViewById(R.id.album).setOnClickListener(this);

        dialog.setContentView(view);
        Window window = dialog.getWindow();

        if (window==null)
        {
            return;
        }
        window.setGravity(Gravity.BOTTOM);

        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.y=20;

        window.setAttributes(attributes);

        dialog.show();


    }
    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (bind != null) {
            bind.unbind();
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent,i);
        dialog.dismiss();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==i)
        {
            ContentResolver contentResolver = this.getContentResolver();
            if (data!=null)
            {
               if (resultCode==RESULT_OK)
               {
                   try {
                       Bitmap bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(data.getData()));
                       image1.setImageBitmap(bitmap);

                       File file = BitMapUtils.getFile(bitmap);

                       RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file.getAbsoluteFile());

                       MultipartBody.Part image = MultipartBody.Part.createFormData("image", file.getName(), requestBody);

                       headPresenter.presenter(image);
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               }
            }
        }
    }

    @Override
    public void onSuccess(HeadBean data) {
        if (data.getStatus().equals("0000"))
        {
            Toast.makeText(MainActivity.this,data.getMessage(),Toast.LENGTH_SHORT).show();
        }else
        {
            Toast.makeText(MainActivity.this,"上传失败",Toast.LENGTH_SHORT).show();

        }
    }

    @Override
    public void onFailter(String msg) {

    }
}
相关标签: 上传头像