android 选着图片,Android 选择图片、上传图片之ImagePicker

酆奇文
2023-12-01

Android 选择图片、上传图片之ImagePicker

最近做一个需求跟微信朋友圈差不多,然后在网上找了一个三方库imagePicker。下面不多说了直接上代码。

先添加依赖:

选择图片:compile 'com.lzy.widget:imagepicker:0.5.4'

github地址:https://github.com/yechaoa/UploadImage

然后创建一个

GlideImageLoader类实现ImageLoader

import android.app.Activity;

import android.net.Uri;

import android.widget.ImageView;

import com.bumptech.glide.Glide;

import com.bumptech.glide.load.engine.DiskCacheStrategy;

import com.lzy.imagepicker.loader.ImageLoader;

import java.io.File;

/**

* Created by yechao on 2017/4/28.

* Describe :

*/

public class GlideImageLoader implements ImageLoader {

@Override

public void displayImage(Activity activity, String path, ImageView imageView, int width, int height) {

Glide.with(activity) //配置上下文

.load(Uri.fromFile(new File(path))) //设置图片路径(fix #8,文件名包含%符号 无法识别和显示)

.diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸

.into(imageView);

}

@Override

public void displayImagePreview(Activity activity, String path, ImageView imageView, int width, int height) {

Glide.with(activity) //配置上下文

.load(Uri.fromFile(new File(path))) //设置图片路径(fix #8,文件名包含%符号 无法识别和显示)

.diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸

.into(imageView);

}

@Override

public void clearMemoryCache() {

//这里是清除缓存的方法,根据需要自己实现

}

}

在这里注意用glide加载图片的话一定要在下面这个方法 里面也配置上,否者会出现预览图片还有不显示图片

@Override

public void displayImagePreview(Activity activity, String path, ImageView imageView, int width, int height) {

Glide.with(activity) //配置上下文

.load(Uri.fromFile(new File(path))) //设置图片路径(fix #8,文件名包含%符号 无法识别和显示)

.diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸

.into(imageView);

}

接下来就是在Activity里面写方法初始化

private void initImagePicker() {

ImagePicker imagePicker = ImagePicker.getInstance();

imagePicker.setImageLoader(new GlideImageLoader()); //设置图片加载器

imagePicker.setShowCamera(true); //显示拍照按钮

imagePicker.setCrop(false); //允许裁剪(单选才有效)

imagePicker.setSaveRectangle(true); //是否按矩形 区域保存

imagePicker.setSelectLimit(maxImgCount); //选中数量限制

imagePicker.setMultiMode(true); //多选

imagePicker.setStyle(CropImageView.Style.RECTANGLE); //裁剪框的形状

imagePicker.setFocusWidth(1000); //裁剪框的宽度。单位像素(圆形自动取宽高最小值)

imagePicker.setFocusHeight(1000); //裁剪框的高度。单位像素(圆形自动取宽高最小值)

imagePicker.setOutPutX(1000); //保存文件的宽度。单位像素

imagePicker.setOutPutY(1000); //保存文件的高度。单位像素

}

这个最好配置在Application的Oncreate()里面配置一次就行

下面这个是MainActivity.java

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.support.v7.widget.GridLayoutManager;

import android.support.v7.widget.RecyclerView;

import android.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.Toast;

import com.lzy.imagepicker.ImagePicker;

import com.lzy.imagepicker.bean.ImageItem;

import com.lzy.imagepicker.ui.ImageGridActivity;

import com.lzy.imagepicker.ui.ImagePreviewDelActivity;

import com.lzy.imagepicker.view.CropImageView;

import java.util.ArrayList;

import java.util.List;

import okhttp3.Call;

public class MainActivity extends AppCompatActivity implements ImagePickerAdapter.OnRecyclerViewItemClickListener {

public static final int IMAGE_ITEM_ADD = -1;

public static final int REQUEST_CODE_SELECT = 100;

public static final int REQUEST_CODE_PREVIEW = 101;

private ImagePickerAdapter adapter;

private ArrayListselImageList; //当前选择的所有图片

private int maxImgCount = 9; //允许选择图片最大数

private HttpUtil httpUtil;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

httpUtil = new HttpUtil();

findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

uploadImage(selImageList);

}

});

//最好放到 Application oncreate执行

initImagePicker();

initWidget();

}

private void initImagePicker() {

ImagePicker imagePicker = ImagePicker.getInstance();

imagePicker.setImageLoader(new GlideImageLoader()); //设置图片加载器

imagePicker.setShowCamera(true); //显示拍照按钮

imagePicker.setCrop(false); //允许裁剪(单选才有效)

imagePicker.setSaveRectangle(true); //是否按矩形 区域保存

imagePicker.setSelectLimit(maxImgCount); //选中数量限制

imagePicker.setMultiMode(true); //多选

imagePicker.setStyle(CropImageView.Style.RECTANGLE); //裁剪框的形状

imagePicker.setFocusWidth(1000); //裁剪框的宽度。单位像素(圆形自动取宽高最小值)

imagePicker.setFocusHeight(1000); //裁剪框的高度。单位像素(圆形自动取宽高最小值)

imagePicker.setOutPutX(1000); //保存文件的宽度。单位像素

imagePicker.setOutPutY(1000); //保存文件的高度。单位像素

}

private void initWidget() {

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

selImageList = new ArrayList<>();

adapter = new ImagePickerAdapter(this, selImageList, maxImgCount);

adapter.setOnItemClickListener(this);

recyclerView.setLayoutManager(new GridLayoutManager(this, 3));

recyclerView.setHasFixedSize(true);

recyclerView.setAdapter(adapter);

}

private SelectDialog showDialog(SelectDialog.SelectDialogListener listener, Listnames) {

SelectDialog dialog = new SelectDialog(this, R.style.transparentFrameWindowStyle, listener, names);

if (!this.isFinishing()) {

dialog.show();

}

return dialog;

}

@Override

public void onItemClick(View view, int position) {

switch (position) {

case IMAGE_ITEM_ADD:

Listnames = new ArrayList<>();

names.add("拍照");

names.add("相册");

showDialog(new SelectDialog.SelectDialogListener() {

@Override

public void onItemClick(AdapterView> parent, View view, int position, long id) {

switch (position) {

case 0: // 直接调起相机

//打开选择,本次允许选择的数量

ImagePicker.getInstance().setSelectLimit(maxImgCount - selImageList.size());

Intent intent = new Intent(MainActivity.this, ImageGridActivity.class);

intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true); // 是否是直接打开相机

startActivityForResult(intent, REQUEST_CODE_SELECT);

break;

case 1:

//打开选择,本次允许选择的数量

ImagePicker.getInstance().setSelectLimit(maxImgCount - selImageList.size());

Intent intent1 = new Intent(MainActivity.this, ImageGridActivity.class);

startActivityForResult(intent1, REQUEST_CODE_SELECT);

break;

default:

break;

}

}

}, names);

break;

default:

//打开预览

Intent intentPreview = new Intent(this, ImagePreviewDelActivity.class);

intentPreview.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, (ArrayList) adapter.getImages());

intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);

intentPreview.putExtra(ImagePicker.EXTRA_FROM_ITEMS, true);

startActivityForResult(intentPreview, REQUEST_CODE_PREVIEW);

break;

}

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {

//添加图片返回

if (data != null && requestCode == REQUEST_CODE_SELECT) {

ArrayListimages = (ArrayList) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);

if (images != null) {

selImageList.addAll(images);

adapter.setImages(selImageList);

for (int i = 0; i < selImageList.size(); i++) {

Log.d("图片地址=", selImageList.get(i).path);

Toast.makeText(getApplicationContext(), "图片地址=" + selImageList.get(i).path, Toast.LENGTH_SHORT).show();

}

}

}

} else if (resultCode == ImagePicker.RESULT_CODE_BACK) {

//预览图片返回

if (data != null && requestCode == REQUEST_CODE_PREVIEW) {

ArrayListimages = (ArrayList) data.getSerializableExtra(ImagePicker.EXTRA_IMAGE_ITEMS);

if (images != null) {

selImageList.clear();

selImageList.addAll(images);

adapter.setImages(selImageList);

}

}

}

}

private String url = "http...";

private void uploadImage(final ArrayListpathList) {

httpUtil.postFileRequest(url, null, pathList, new MyStringCallBack() {

@Override

public void onError(Call call, Exception e, int id) {

super.onError(call, e, id);

}

@Override

public void onResponse(String response, int id) {

super.onResponse(response, id);

//返回图片的地址

}

});

}

}

github地址:https://github.com/yechaoa/UploadImage

 类似资料: