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

上传头像

程序员文章站 2024-03-25 12:51:10
...
package com.uploadapp;

import android.content.DialogInterface;
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.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static String path = "/sdcard/myHead/";//sd路径
    private ImageView ivPhoto;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ivPhoto = (ImageView) findViewById(R.id.iv_photo);
        findViewById(R.id.iv_photo).setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        AlertDialog builder = new AlertDialog.Builder(MainActivity.this)
                .setTitle("请选择")
                .setNegativeButton("相机", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                                "head.jpg")));
                    }
                }) .setPositiveButton("相册", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // xiangce();
                        //从相册里选
                        Intent intent1 = new Intent(Intent.ACTION_PICK, null);
                        intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                        startActivityForResult(intent1, 1);
                    }
                })
                .create();
        builder.show();

    }
    /*@Override
    public void onClick(View view) {
        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                .setTitle("请选择")
                .setNegativeButton("相机", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //init();
                        //拍照
                        Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                                "head.jpg")));
                        startActivityForResult(intent2, 2);//采用ForResult打开
                    }
                })
                .setPositiveButton("相册", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // xiangce();
                        //从相册里选
                        Intent intent1 = new Intent(Intent.ACTION_PICK, null);
                        intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                        startActivityForResult(intent1, 1);
                    }
                })
                .create();
        alertDialog.show();
    }*/
    /**
     * 调用系统的裁剪
     *
     * @param uri
     */
    public void cropPhoto(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 3);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    cropPhoto(data.getData());//裁剪图片
                }
                break;
            case 2:
                if (resultCode == RESULT_OK) {
                    File temp = new File(Environment.getExternalStorageDirectory()
                            + "/head.jpg");
                    cropPhoto(Uri.fromFile(temp));//裁剪图片
                }
                break;
            case 3:
                if (data!=null){
                    Bundle bundle=data.getExtras();
                    if (bundle==null){
                        return;
                    }
                    Bitmap head=bundle.getParcelable("data");
                    if (head!=null){
                        ivPhoto.setImageBitmap(head);
                        String fileName=path+"/head.jpg";
                        upload(fileName);
                    }
                }
                break;
        }

    }
    private String upUrl="/small/user/verify/v1/modifyHeadPic";
    private void upload(String sdPath){
        Map<String,String> headmap1=new HashMap<>();
        headmap1.put("userId","2325");
        headmap1.put("sessId","15565177104002325");
        new RetrofitUtils().upload(upUrl,headmap1,sdPath).result(new RetrofitUtils.HttpListener() {
            @Override
            public void success(String data) {
                Toast.makeText(MainActivity.this, data, Toast.LENGTH_LONG).show();

            }

            @Override
            public void fail(String error) {
                Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();

            }
        });

    }


   /* private String upUrl1="/small/user/verify/v1/modifyHeadPic";
    private void upLoad(String sdPath){
        Map<String,String> headmap=new HashMap<>();
        headmap.put("userId","2325");
        headmap.put("sessId","15565177104002325");
        new RetrofitUtils().upload(upUrl1,headmap,sdPath).result(new RetrofitUtils.HttpListener() {
            @Override
            public void success(String data) {
                Toast.makeText(MainActivity.this, data, Toast.LENGTH_LONG).show();

            }

            @Override
            public void fail(String error) {
                Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();

            }
        });
    }*/
   /*  //上传
    private String upUrl = "/small/user/verify/v1/modifyHeadPic";//上传
    private void upLoad(String sdPath) {
        Map<String, String> headMap = new HashMap<>();
        headMap.put("userId", "2325");
        headMap.put("sessionId", "15565177104002325");
        new RetrofitUtils().upload(upUrl, headMap, sdPath).result(new RetrofitUtils.HttpListener() {
            @Override
            public void success(String data) {
                Toast.makeText(MainActivity.this, data, Toast.LENGTH_LONG).show();
            }

            @Override
            public void fail(String error) {
                Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
            }
        });


    }*/

    /*//保存到SD卡
    private void setPicToView(Bitmap mBitmap) {
        String sdStatus = Environment.getExternalStorageState();
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
            return;
        }
        FileOutputStream b = null;
        File file = new File(path);
        file.mkdirs();// 创建文件夹
        String fileName = path + "/head.jpg";//图片名字
        try {
            b = new FileOutputStream(fileName);
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭流
                b.flush();
                b.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }*/




}

相关标签: 头像上传