Android 手寫(xiě)手機(jī)圖片選擇器

效果圖

photo.png

需求: 獲取手機(jī)內(nèi)存中的所有圖片,以及拍照?qǐng)D片。

首頁(yè)布局RecyclerView,就不上布局代碼了。

  • 直接上整體首頁(yè)代碼
public class PhotoListActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView recycManager;
    private LoaderManager.LoaderCallbacks<Cursor> loaderCallbacks;//圖片加載器
    private Activity mActivity = null;
    private ArrayList<PhotoInfo> photoInfoList = new ArrayList<>();
    private PhotoAdapter photoAdapter;
    private File path;
    private TextView textNumber;
    private TextView textEnter;
    private ArrayList<String> resultList = new ArrayList<>();//返回首頁(yè)的集合

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_list);
        initView();
        initListener();
        initPhoto();
        initRecycler();
    }

    private void initView() {
        mActivity = this;
        recycManager = (RecyclerView) findViewById(R.id.recycManager);
        textNumber = (TextView) findViewById(R.id.textNumber);
        textEnter = (TextView) findViewById(R.id.textEnter);
        textNumber.setText("(" + 0 + "/9" + ")");
    }

    private void initListener() {
        textEnter.setOnClickListener(this);
    }

    private File createCreamePath;

    private void initRecycler() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mActivity, 3);
        recycManager.setLayoutManager(gridLayoutManager);
        photoAdapter = new PhotoAdapter(mActivity, photoInfoList);
        recycManager.setAdapter(photoAdapter);

        photoAdapter.getPhoto(new PhotoAdapter.getPhoto() {
            @Override
            public void getTakeCreame(View v) {
                Intent intentPhone = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                createCreamePath = createTmpFile(mActivity, "/Edreamoon/Pictures");
                intentPhone.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(createCreamePath));
                intentPhone.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
                startActivityForResult(intentPhone, 100);
            }
        });

        photoAdapter.getSelectPhoto(new PhotoAdapter.getSelectPhoto() {
            @Override
            public void getListPhoto(List<String> photoPath) {
                resultList.clear();
                int size = photoPath.size();
                textNumber.setText("(" + size + "/9" + ")");
                if (size > 0) {
                    textEnter.setVisibility(View.VISIBLE);
                } else {
                    textEnter.setVisibility(View.GONE);
                }

                //獲取選擇集合賦值給 返回MAIN集合
                for (String path : photoPath) {
                    if (resultList.contains(path)) {
                        resultList.remove(path);
                    } else {
                        resultList.add(path);
                    }
                }
            }
        });
    }

private void initPhoto() {
    loaderCallbacks = new LoaderManager.LoaderCallbacks<Cursor>() {
        private final String[] IMAGE_PROJECT = {
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.DATE_ADDED,
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.SIZE,
        };

        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            if (id == 0) {
                return new CursorLoader(mActivity, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_PROJECT, null, null, IMAGE_PROJECT[2] + " DESC");

            } else if (id == 1) {
                return new CursorLoader(mActivity, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_PROJECT, IMAGE_PROJECT[0] + "like '%" + args.getString("path") + "%'", null, IMAGE_PROJECT[2] + " DESC");
            }
            return null;
        }

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            if (data != null) {
                photoInfoList.clear();
                int count = data.getCount();
                data.moveToFirst();
                if (count > 0) {
                    do {
                        String path = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECT[0]));
                        String name = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECT[1]));
                        long time = data.getLong(data.getColumnIndexOrThrow(IMAGE_PROJECT[2]));
                        int size = data.getInt(data.getColumnIndexOrThrow(IMAGE_PROJECT[4]));
                        PhotoInfo photoInfo = new PhotoInfo(path, name, time, false);
                        Boolean isSize5k = size > 1024 * 5;
                        if (isSize5k) {
                            photoInfoList.add(photoInfo);
                        }
                    } while (data.moveToNext());
                }
            }
        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {

        }
    };
    getSupportLoaderManager().restartLoader(0, null, loaderCallbacks);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100 && resultCode == RESULT_OK) {
        resultList.clear();
        String absolutePath = createCreamePath.getAbsolutePath();
        resultList.add(absolutePath);
        Intent intent = new Intent(mActivity, MainActivity.class);
        intent.putExtra("resultPath", resultList);
        setResult(200, intent);
        finish();
    }
}
    /**
     * 創(chuàng)建文件
     *
     * @param context  context
     * @param filePath 文件路徑
     * @return file
     */
    public static File createTmpFile(Context context, String filePath) {

        String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA).format(new Date());

        String externalStorageState = Environment.getExternalStorageState();

        File dir = new File(Environment.getExternalStorageDirectory() + filePath);

        if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
            if (!dir.exists()) {
                dir.mkdirs();
            }
            return new File(dir, timeStamp + ".jpg");
        } else {
            File cacheDir = context.getCacheDir();
            return new File(cacheDir, timeStamp + ".jpg");
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.textEnter:
                Intent intent = new Intent(mActivity, MainActivity.class);
                intent.putExtra("resultPath", resultList);
                setResult(200, intent);
                finish();
                break;
        }
    }
}
  • adapter代碼
public class PhotoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<PhotoInfo> list;
    private LayoutInflater inflater;
    private getPhoto getPhoto;
    private List<String> selectPhoto = new ArrayList<>();
    private getSelectPhoto getSelectPhoto;

    public PhotoAdapter(Context mContext, List<PhotoInfo> list) {
        this.mContext = mContext;
        this.list = list;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        selectPhoto.clear();
    }

    public void getPhoto(getPhoto getPhoto) {
        this.getPhoto = getPhoto;
    }

    public void getSelectPhoto(getSelectPhoto getSelectPhoto) {
        this.getSelectPhoto = getSelectPhoto;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {//顯示的第一張圖片的布局 , 就是調(diào)用攝像頭拍照的布局。
            return new MyViewHolder(inflater.inflate(R.layout.item_photo_adapter_first, parent, false));
        }//之后的圖片顯示的布局。
        return new MyViewHolder(inflater.inflate(R.layout.item_photo_adapter, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        holder.setIsRecyclable(false);
        ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
        params.height = getScreenWidth(mContext) / 3;
        params.width = getScreenWidth(mContext) / 3;
        holder.itemView.setLayoutParams(params);//重設(shè)item大小

        if (getItemViewType(position) == 0) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //因?yàn)榕恼找祷貐?shù),所以接口回調(diào)方式將事件處理交給activity處理
                    getPhoto.getTakeCreame(v);
                }
            });
            return;
        }

        final MyViewHolder myViewHolder = (MyViewHolder) holder;
        final PhotoInfo photoInfo = list.get(position - 1);
        Glide.with(mContext).load(photoInfo.path).into(myViewHolder.image);

        myViewHolder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectPhoto.size() < 9) {
                    //selectPhoto為選中的集合。
                    if (selectPhoto.contains(photoInfo.path)) {
                        selectPhoto.remove(photoInfo.path);
                        photoInfo.setChecked(false);
                        myViewHolder.checkBox.setButtonDrawable(R.mipmap.uncheck);

                    } else {
                        selectPhoto.add(photoInfo.path);
                        photoInfo.setChecked(true);
                        myViewHolder.checkBox.setButtonDrawable(R.mipmap.checked);
                    }
                    myViewHolder.checkBox.setChecked(photoInfo.getChecked());
                    getSelectPhoto.getListPhoto(selectPhoto);
                } else {
                    if (selectPhoto.contains(photoInfo.path)) {
                        selectPhoto.remove(photoInfo.path);
                        photoInfo.setChecked(false);
                        myViewHolder.checkBox.setButtonDrawable(R.mipmap.uncheck);

                    } else {
                        Toast.makeText(mContext, "最多選擇9張", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        if (photoInfo.getChecked()) {
            myViewHolder.checkBox.setChecked(photoInfo.getChecked());
            myViewHolder.checkBox.setButtonDrawable(R.mipmap.checked);
        } else {
            myViewHolder.checkBox.setButtonDrawable(R.mipmap.uncheck);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        }
        return 1;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView image;
        private CheckBox checkBox;

        public MyViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
            checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);
        }
    }

    /**
     * 獲得屏幕高度
     *
     * @param context context
     * @return 屏幕高度
     */
    public int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    //獲取拍照后的照片
    public interface getPhoto {
        void getTakeCreame(View v);
    }

    public interface getSelectPhoto {
        void getListPhoto(List<String> photoPath);
    }
}

  • 模型類(lèi)
public class PhotoInfo {

    public String name;                 // 圖片名
    public String path;                 // 圖片路徑
    public long time;                   // 圖片添加時(shí)間
    public Boolean checked;             //checkbox  選中狀態(tài)

    public PhotoInfo(String path, String name, long time ,Boolean checked) {
        this.path = path;
        this.name = name;
        this.time = time;
        this.checked =checked;
    }


    public Boolean getChecked() {
        return checked;
    }

    public void setChecked(Boolean checked) {
        this.checked = checked;
    }

    @Override
    public String toString() {
        return "PhotoInfo{" +
                "name='" + name + '\'' +
                ", path='" + path + '\'' +
                ", time=" + time +
                '}';
    }

    @Override
    public boolean equals(Object object) {
        try {
            PhotoInfo other = (PhotoInfo) object;
            return this.path.equalsIgnoreCase(other.path);
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        return super.equals(object);
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }
}
  • 具體思路
    1-寫(xiě)一個(gè)模型類(lèi)儲(chǔ)存自己想要的數(shù)據(jù): 路徑,文件名,時(shí)間
    2-涉及到recyclerView復(fù)用套checkbox的問(wèn)題,所以在模型類(lèi)中加了Boolean類(lèi)型顯示checkbox狀態(tài)
    3-通過(guò)LoaderManager.LoaderCallbacks<Cursor> 獲取內(nèi)存圖片
    4-書(shū)寫(xiě)adapter :由于調(diào)用本地拍照需返回,所以adapter寫(xiě)接口,講事件處理交給activity
    5-加部分細(xì)節(jié),比如只能只能9張 ,等。
    6-activityResult回調(diào)處理,我將路徑全都存到了集合中,可直接拿來(lái)路徑上傳服務(wù)器等或別的操作

csdn
https://blog.csdn.net/binbinxiaoz

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容