LiangGaoUser / AndroidEbook

0 stars 0 forks source link

如何上传头像 #13

Open LiangGaoUser opened 4 years ago

LiangGaoUser commented 4 years ago

https://github.com/LiangGaoUser/PictureSelector https://github.com/LiangGaoUser/PictureSelectorDemo 使用pictureselector来上传图片,使用Pictureselector可以上传图片,音频,视频 使用也比较方面


package com.lianggao.whut.fifthtest;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.permissions.Permission;
import com.luck.picture.lib.permissions.RxPermissions;
import com.luck.picture.lib.tools.DateUtils;
import com.luck.picture.lib.tools.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

public class MainActivity5 extends AppCompatActivity {

    private ImageView imageView;
    private ImageView imageView2;
    private Button button;
    private Button button2;
    private View view;
    private static Bitmap drawingCache=null;
    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);

        imageView=(ImageView)findViewById(R.id.id_iv);
        imageView.setDrawingCacheEnabled(true);
        imageView2=(ImageView)findViewById(R.id.id_iv2);
        button=(Button)findViewById(R.id.id_btn);
        button2=(Button)findViewById(R.id.id_btn2);
        view=(View)findViewById(R.layout.activity_main5);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PictureSelector.create(MainActivity5.this)
                        .openGallery(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .minSelectNum(1)
                        .imageSpanCount(4)
                        .selectionMode(PictureConfig.MULTIPLE)
                        .forResult(PictureConfig.CHOOSE_REQUEST);

            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Bitmap obmp = Bitmap.createBitmap(imageView.getDrawingCache());

                //Bitmap obmp = imageView.getDrawingCache();
                imageView2.setImageBitmap(obmp);
                imageView.setDrawingCacheEnabled(false);//这里一定要清楚图片的缓存
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<LocalMedia> images;
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片选择结果回调

                images = PictureSelector.obtainMultipleResult(data);
                LocalMedia media =images.get(0);
                int mimeType = media.getMimeType();
                String path;
                path = media.getPath();

                Log.i("原图地址::", media.getPath());
                int pictureType = PictureMimeType.isPictureType(media.getPictureType());

                long duration = media.getDuration();

                imageView.setDrawingCacheEnabled(true);
                RequestOptions options = new RequestOptions()
                        .centerCrop()
                        .placeholder(R.color.color_f6)
                        .diskCacheStrategy(DiskCacheStrategy.ALL);
                Glide.with(this.getApplicationContext())
                        .load(path)
                        .apply(options)
                        .into(imageView);

               // drawingCache =Bitmap.createBitmap(imageView.getDrawingCache());
               /* ByteArrayOutputStream baos = new ByteArrayOutputStream();
                drawingCache.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                System.out.println("&&&"+baos.toString());*/
                //Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

            }
        }
    }

}

较复杂的例子参考demo