• 先看效果图。 直接上代码。主要是Adapter这边。public class GvAdapter extends BaseAdapter{ private Context context; private int mMaxPosition;//根据这个list.size+1 来进行判断 private List<String> list...

    先看效果图。
    这里写图片描述

    直接上代码。主要是Adapter这边。

    public class GvAdapter extends BaseAdapter{
        private Context context;
        private int mMaxPosition;//根据这个list.size+1 来进行判断
        private List<String> list;
    
        public GvAdapter(Context context, List<String> list) {
            this.context = context;
            this.list = list;
        }
    
        @Override
        public int getCount() {
            mMaxPosition=list.size()+1;
            return mMaxPosition;
        }
        //根据情况可以设置是否需要。。
        public int getMaxPosition(){
            return mMaxPosition;
        }
        @Override
        public Object getItem(int position) {
            return list.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(final int position, View v, ViewGroup
                parent) {
            ViewHolder vh=null;
            if (v==null){
                vh=new ViewHolder();
                v= LayoutInflater.from(context).inflate(R.layout.item_gd,parent,false);
                vh.img= (ImageView) v.findViewById(R.id.img);
                vh.demimg= (ImageView) v.findViewById(R.id.delimg);
                v.setTag(vh);
            }else{
                vh= (ViewHolder) v.getTag();
            }
            if (position==mMaxPosition-1){ //说明要显示
                Glide.with(context).load(R.drawable.id_photo).dontAnimate()
                        .centerCrop().into(vh.img);
                vh.img.setVisibility(View.VISIBLE);
                vh.demimg.setVisibility(View.GONE);
                if (position==6&&mMaxPosition==7){//设置最大6个。那么达到最大,就隐藏。
    //                vh.img.setImageResource(R.drawable.id_photo);
                    vh.img.setVisibility(View.GONE);
                }
            }else{//设置图片。
                vh.demimg.setVisibility(View.VISIBLE);
                Glide.with(context).load(list.get(position)).into(vh.img);//设置
            }
            //删除
            vh.demimg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    list.remove(position);
                    notifyDataSetChanged();
                }
            });
            return v;
        }
    
        public class ViewHolder{
            public ImageView img,demimg;
        }

    //这里引用了一个很好用的相册框架

     //相册
        compile 'com.linchaolong.android:imagepicker:1.2'
    //加载
        compile 'com.github.bumptech.glide:glide:3.8.0'
    public class MainActivity extends AppCompatActivity {
        private GridView gridView;
        private GvAdapter adapter;
        private List<String> list;
        ImagePicker imagePicker ;
        String path;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            gridView = (GridView) findViewById(R.id.grid_view);
            initView();
        }
        private void initView() {
            imagePicker = new ImagePicker();
            // 设置标题
            imagePicker.setTitle("设置头像");
            // 设置是否裁剪图片
            imagePicker.setCropImage(true);
            list = new ArrayList<>();
            adapter = new GvAdapter(this, list);
            gridView.setAdapter(adapter);
            gridView.setOnItemClickListener(new AdapterView
                    .OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View
                        view, int position, long id) {
                    //判断是否是最后一个。
                    if (position==parent.getChildCount()-1){
                        if (position==6){//不能点击了
                        }else{
                            opnePhoto();
                        }
                    }else{//可以加点预览功能。
    
                    }
                }
            });
        }
        public void opnePhoto() {
            // 启动图片选择器
            imagePicker.startChooser(this, new ImagePicker.Callback() {
                // 选择图片回调
                @Override
                public void onPickImage(Uri imageUri) {
                }
                // 裁剪图片回调
                @Override
                public void onCropImage(Uri imageUri) {
                    if (list.size()>=6){
                        Toast.makeText(MainActivity.this,"最多选择六张图片",Toast.LENGTH_LONG).show();
                    }else{
                        list.add(String.valueOf(imageUri));
                    }
                    adapter.notifyDataSetChanged();
    //                path= String.valueOf(imageUri);
                }
    
                // 自定义裁剪配置
                @Override
                public void cropConfig(CropImage.ActivityBuilder
                                               builder) {
                    builder
                            // 是否启动多点触摸
                            .setMultiTouchEnabled(false)
                            // 设置网格显示模式
                            .setGuidelines(CropImageView.Guidelines.OFF)
                            // 圆形/矩形
                            .setCropShape(CropImageView.CropShape
                                    .RECTANGLE)
                            // 调整裁剪后的图片最终大小
                            .setRequestedSize(960, 540)
                            // 宽高比
                            .setAspectRatio(16, 9);
                }
                // 用户拒绝授权回调
                @Override
                public void onPermissionDenied(int requestCode,
                                               String[] permissions,
                                               int[] grantResults) {
                }
            });
        }
        @Override
        protected void onActivityResult(int requestCode, int
                resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            imagePicker.onActivityResult(MainActivity.this,requestCode, resultCode, data);
    
        }
    }

    这是xml界面

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="theagent.com.photo.MainActivity">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"/>
        <GridView
            android:id="@+id/grid_view"
            android:numColumns="4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </GridView>
    </LinearLayout>
    

    这里是demo下载

    展开全文
  • 网络图片,实现0张,或者多张图片显示的开源框架 https://github.com/emilsjolander/StickyListHeaders 关于怎样运用到自己的项目中去,下面是我参考的大神的帖子 ...

    网络图片,实现0张,或者多张图片显示的开源框架

    https://github.com/emilsjolander/StickyListHeaders

    关于怎样运用到自己的项目中去,下面是我参考的大神的帖子

    http://www.cnblogs.com/android100/p/StickyGridHeaders.html

    展开全文
  • Android 多张图片展示,仿微信图片上传,可以选择多张图片

    我们经常会遇到需要多张图片展示上传的需求 ,如图
    这里写图片描述

    这样的需求我已经遇到过多次,个人总结一下,希望大家多多指点,支持选择多张图片
    这里写图片描述

    布局:一个GridView

     <com.zuihou.drunkenmonkey.widget.view.DirectoryScrollGridView
                        android:id="@+id/gridView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:numColumns="3"
                        android:padding="@dimen/margin_8" />

    点击从相册或相机选择图片出现弹框,弹框布局如下:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:id="@+id/ll_popup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/homepage_type_bg"
            android:orientation="vertical">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:orientation="horizontal">
    
                <Button
                    android:id="@+id/item_popupwindows_camera"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:text="拍照"
                    android:textColor="#585858"
                    android:textSize="@dimen/font_16" />
            </LinearLayout>
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1px"
                android:layout_marginLeft="80dp"
                android:background="#f2f2f2" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:orientation="horizontal">
    
                <Button
                    android:id="@+id/item_popupwindows_Photo"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:text="从相册中选取"
                    android:textColor="#585858"
                    android:textSize="@dimen/font_16" />
            </LinearLayout>
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="#f3f3f3" />
    
            <Button
                android:id="@+id/item_popupwindows_cancel"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:text="取消"
                android:textColor="#585858"
                android:textSize="@dimen/font_16" />
        </LinearLayout>
    
    </RelativeLayout>
    

    点击从相册或相机选择图片出现弹框,Activity代码:

     /**
         * 底部弹框选择类型
         */
        private void pictureSelect() {
            View view = getLayoutInflater().inflate(R.layout.llj_item_popupwindows, null);
            // 设置style 控制默认dialog带来的边距问题
            final Dialog dialog = new Dialog(this, R.style.common_dialog);
            dialog.setContentView(view);
            dialog.show();
    
            Button camera = (Button) view.findViewById(R.id.item_popupwindows_camera);
            final Button photo = (Button) view.findViewById(R.id.item_popupwindows_Photo);
            Button cancel = (Button) view.findViewById(R.id.item_popupwindows_cancel);
    
            //手机拍照
            camera.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    photo();
                    dialog.dismiss();
                }
            });
    
            //从手机选择
            photo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int selectedMode;
                    selectedMode = MultiImageSelectorActivity.MODE_MULTI;
                    int maxNum = 3 - drr.size();
                    Intent intent = new Intent(AdviceActivity.this, MultiImageSelectorActivity.class);
                    // 是否显示拍摄图片
                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, false);
                    // 最大可选择图片数量
                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, maxNum);
                    // 选择模式
                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, selectedMode);
    
                    startActivityForResult(intent, RESULT_LOAD_IMAGE);
                    dialog.dismiss();
    
                }
            });
    
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
    
            // 设置相关位置,一定要在 show()之后
            Window window = dialog.getWindow();
            window.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            params.gravity = Gravity.BOTTOM;
            window.setAttributes(params);
        }
    

    拍照:

    /**
         * 拍照
         */
        public void photo() {
            try {
                Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                String sdcardState = Environment.getExternalStorageState();
                String sdcardPathDir = Environment.getExternalStorageDirectory().getPath() + "/tempImage/";
                File file = null;
                if (Environment.MEDIA_MOUNTED.equals(sdcardState)) {
                    // 有sd卡,是否有myImage文件夹
                    File fileDir = new File(sdcardPathDir);
                    if (!fileDir.exists()) {
                        fileDir.mkdirs();
                    }
                    // 是否有headImg文件
                    file = new File(sdcardPathDir + System.currentTimeMillis() + ".JPEG");
                }
                if (file != null) {
                    path = file.getPath();
                    photoUri = Uri.fromFile(file);
                    openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
    
                    startActivityForResult(openCameraIntent, TAKE_PICTURE);
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    选择照片返回结果

     private static final int TAKE_PICTURE = 0;
        private static final int RESULT_LOAD_IMAGE = 1;
        private static final int CUT_PHOTO_REQUEST_CODE = 2;
        private String path = "";
        private Uri photoUri;
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
                case TAKE_PICTURE:
                    if (drr.size() < 3 && resultCode == -1) {
                        startPhotoZoom(photoUri);
                    }
                    break;
                case RESULT_LOAD_IMAGE:
                    if (drr.size() < 3 && resultCode == RESULT_OK && null != data) {
                        mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                        Log.e("6666", ":" + mSelectPath);
                        Bitmap bitmap;
    
                        try {
                            for (String p : mSelectPath) {
                                Log.e("55555", p.toString());
                                bitmap = Bimp.revitionImageSize(p.toString());
                                PhotoActivity.bitmap.add(bitmap);
                                bitmap = Bimp.createFramedPhoto(480, 480, bitmap, (int) (dp * 1.6f));
                                bmp.add(bitmap);
                                gridviewInit();
                                //把图片数量添加进集合,方便删除,统计数量
                                drr.add(FileUtils.SDPATH + ".JPEG");
    
                                //把位图转化为字符流
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                                baos.close();
                                byte[] buffer = baos.toByteArray();
                                String[] photoImgliu = new String[1024 * 1024];
                                photoImgliu[i] = Base64.encodeToString(buffer, 0, buffer.length, Base64.DEFAULT);
                                Log.e("666", "photoLiu" + i + ":" + photoImgliu[i]);
                                i++;
                            }
    
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case CUT_PHOTO_REQUEST_CODE:
                    if (resultCode == RESULT_OK && null != data) {
                        Bitmap bitmap = Bimp.getLoacalBitmap(drr.get(drr.size() - 1));
                        PhotoActivity.bitmap.add(bitmap);
                        bitmap = Bimp.createFramedPhoto(480, 480, bitmap, (int) (dp * 1.6f));
                        bmp.add(bitmap);
    
                        gridviewInit();
    
                        try {
                            //把位图转化为字符流
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                            baos.close();
                            byte[] buffer = baos.toByteArray();
                            String[] cameralImgliu = new String[1024 * 1024];
    
                            cameralImgliu[i] = Base64.encodeToString(buffer, 0, buffer.length, Base64.DEFAULT);
                            Log.e("666", "camearlLiu" + i + ":" + cameralImgliu[i]);
                            i++;
    
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
    
                default:
                    break;
            }
        }

    选取照片后截图:

     private void startPhotoZoom(Uri uri) {
            try {
                // 获取系统时间 然后将裁剪后的图片保存至指定的文件夹
                SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
                String address = sDateFormat.format(new Date());
    
                if (!FileUtils.isFileExist("")) {
                    FileUtils.createSDDir("");
                }
    
                drr.add(FileUtils.SDPATH + address + ".JPEG");
                Uri imageUri = Uri.parse("file:///sdcard/formats/" + address + ".JPEG");
                final Intent intent = new Intent("com.android.camera.action.CROP");
                // 照片URL地址
                intent.setDataAndType(uri, "image/*");
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                intent.putExtra("outputX", 480);
                intent.putExtra("outputY", 480);
                // 输出路径
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                // 输出格式
                intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                // 不启用人脸识别
                intent.putExtra("noFaceDetection", false);
                intent.putExtra("return-data", false);
                startActivityForResult(intent, CUT_PHOTO_REQUEST_CODE);
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    适配器的代码如下:

    /**
         * gridview适配器
         */
        public class GridAdapter extends BaseAdapter {
            private LayoutInflater listContainer;
            private int selectedPosition = -1;
            private boolean shape;
    
            public boolean isShape() {
                return shape;
            }
    
            public void setShape(boolean shape) {
                this.shape = shape;
            }
    
            public class ViewHolder {
                public ImageView image;
                public Button bt;
            }
    
            public GridAdapter(Context context) {
                listContainer = LayoutInflater.from(context);
            }
    
            @Override
            public int getCount() {
                if (bmp.size() < 3) {
                    return bmp.size() + 1;
                } else {
                    return bmp.size();
                }
            }
    
            @Override
            public Object getItem(int arg0) {
                return null;
            }
    
            @Override
            public long getItemId(int arg0) {
                return 0;
            }
    
            public void setSelectedPosition(int position) {
                selectedPosition = position;
            }
    
            public int getSelectedPosition() {
                return selectedPosition;
            }
    
            /**
             * ListView Item设置
             */
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                final int sign = position;
                GridAdapter.ViewHolder holder = null;
                if (convertView == null) {
                    holder = new GridAdapter.ViewHolder();
                    convertView = listContainer.inflate(R.layout.design_upload, null);
                    holder.image = (ImageView) convertView.findViewById(R.id.img);
                    holder.bt = (Button) convertView.findViewById(R.id.item_bt);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
    
                if (position == bmp.size()) {
                    holder.image.setImageBitmap(BitmapFactory.decodeResource(
                            getResources(), R.mipmap.ic_photo));
                    holder.bt.setVisibility(View.GONE);
                    if (position == 3) {
                        holder.image.setVisibility(View.GONE);
                    }
                } else {
                    holder.image.setImageBitmap(bmp.get(position));
                    holder.bt.setVisibility(View.VISIBLE);
                    holder.bt.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            PhotoActivity.bitmap.remove(sign);
                            bmp.get(sign).recycle();
                            bmp.remove(sign);
                            drr.remove(sign);
    
                            gridviewInit();
                        }
                    });
                }
                return convertView;
            }
        }

    源码可以下载,http://download.csdn.net/download/qq_23668969/10130344

    展开全文
  • Android 实现拍照和调用相册取多张图片,以下只展示两张图片

    一、使用:https://github.com/lovetuzitong/MultiImageSelector,实现拍照和调用相册功能,得到多张图片地址。
    二、展示两张图片
    总结:调用相册没记忆功能,展示多张图片可在步骤二自行修改实现

    这里写图片描述

    这里写图片描述
    **

    步骤一:拍照和调用相册,获取多张图片地址

    **

    https://github.com/lovetuzitong/MultiImageSelector
    下载里面的文件 multi-image-selector,添加到自己的工程中。

    1、android studio 导入Module并添加依赖
    导入:File–>New–>Import Module
    依赖:File–>Project Structure–>app–>Dependencies–>点击加号,选第三个
    2、权限

        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    
      <application
    
            <activity
                android:configChanges="orientation|screenSize"
                android:name="me.nereo.multi_image_selector.MultiImageSelectorActivity" />
        </application>
    3、点击控件,打开相册,获取图片地址
    
             Intent intent=new Intent(mContext, MultiImageSelectorActivity.class);
            // 是否显示调用相机拍照
            intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA,true);
            //设置获取最大图片数量
            intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT,2);
            // 设置模式(单选 MODE_SINGLE /多选MODE_MULTI)
            intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE,MultiImageSelectorActivity.MODE_MULTI);
            startActivityForResult(intent,REQUEAT_CODE);
    4、在回调函数onActivityResult()方法中,处理返回的数据
    
      switch (requestCode){
                //图片返回
                case REQUEAT_CODE:
                    if (data!=null){
                        ArrayList<String> mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                        //得到照片的路径,以下处理具体逻辑
    
    结论:可以从相册得到多张图片的地址。
    

    **

    步骤二:展示两张图片

    **

     以下为具体逻辑
    
        for (int i=0;i<mSelectPath.size();i++){
                            if (i==0&& rela_up_picture1.getVisibility()==View.GONE){
                                Bitmap bt = BitmapFactory.decodeFile(mSelectPath.get(i).toString());
                                iv_up_picture1.setImageBitmap(ThumbnailUtils.extractThumbnail(bt,100,80));
                                rela_up_picture1.setVisibility(View.VISIBLE);
                                tv_remember_path1.setText(mSelectPath.get(i).toString());
                            }else if (i==0&& rela_up_picture1.getVisibility()==View.VISIBLE){
                                Bitmap bt = BitmapFactory.decodeFile(mSelectPath.get(i).toString());
                                iv_up_picture2.setImageBitmap(ThumbnailUtils.extractThumbnail(bt,100,80));
                                rela_up_picture2.setVisibility(View.VISIBLE);
                                iv_up_picture.setVisibility(View.GONE);
                                tv_remember_path2.setText(mSelectPath.get(i).toString());
                            } else if (i==1){
                                Bitmap bt = BitmapFactory.decodeFile(mSelectPath.get(i).toString());
                                iv_up_picture2.setImageBitmap(ThumbnailUtils.extractThumbnail(bt,100,80));
                                rela_up_picture2.setVisibility(View.VISIBLE);
                                iv_up_picture.setVisibility(View.GONE);
                                tv_remember_path2.setText(mSelectPath.get(i).toString());
                            }else if (i==0&& rela_up_picture2.getVisibility()==View.VISIBLE){
                                Bitmap bt= BitmapFactory.decodeFile(mSelectPath.get(i).toString());
                                iv_up_picture1.setImageBitmap(ThumbnailUtils.extractThumbnail(bt,105,70));
                                rela_up_picture1.setVisibility(View.VISIBLE);
                                iv_up_picture.setVisibility(View.GONE);
                                tv_remember_path1.setText(mSelectPath.get(i).toString());
                            }
    
                            if (rela_up_picture2.getVisibility() == View.VISIBLE) {
                                btn_up_delete1.setVisibility(View.GONE);
                            }else {
                                btn_up_delete1.setVisibility(View.VISIBLE);
                            }
                        }
    布局是三张图片,后两张用于展示。
    步骤二是我自己写的,暂时还没发现问题,有需要的可以找我。
    
    展开全文
  • 一分钟实现Android多张图片选择

    图片选择器, 支持多图选择和图片预览等功能。

    开源地址:https://github.com/open-android/ImageSelector

    1. 支持jitpack
    2. 支持选择多张
    3. 支持选择图片数量上限
    4. 支持图片选择顺序
    5. 支持图片预览

    多图选择.gif


    使用步骤

    1. 在project的build.gradle添加如下代码(如下图)

    allprojects {
        repositories {
            maven { url "https://jitpack.io" }
        }
    }
    

    2. 在Module的build.gradle添加依赖

      compile 'com.github.open-android:ImageSelector:0.1.0'
    

    3. 配置如下权限

     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.CAMERA"/>
    

    4.复制如下代码到xml布局

     <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="selectImg"
        android:text="@string/image_selector_select_image" />
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </ScrollView>
    

    5.复制如下代码到java类当中

    public class MainActivity extends AppCompatActivity {
    
        private static final int REQUEST_CODE_SELECT_IMG = 1;
        private static final int MAX_SELECT_COUNT = 9;
        private TextView mContentTv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
    
        private void initView() {
            mContentTv = (TextView) findViewById(R.id.content);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_CODE_SELECT_IMG) {
                showContent(data);
                return;
            }
    
            super.onActivityResult(requestCode, resultCode, data);
        }
    
        private void showContent(Intent data) {
            List<String> paths = ImageSelector.getImagePaths(data);
            if (paths.isEmpty()) {
                mContentTv.setText(R.string.image_selector_select_none);
                return;
            }
    
            mContentTv.setText(paths.toString());
        }
    
        public void selectImg(View v) {
            ImageSelector.show(this, REQUEST_CODE_SELECT_IMG, MAX_SELECT_COUNT);
        }}
    

    欢迎关注微信公众号

    展开全文
  • 添加依赖: compile 'me.iwf.photopicker:PhotoPicker:0.1.8' compile 'com.jaeger.ninegridimageview:library:1.0.1' compile 'com.youth.banner:banner:1.4.10' ...public class MainActivity extend...
  • Android设备上一张图片显示过程 应用示例 ...假如我们现在有一张这样的风景照  ...想在Android设备(比如一个小米pad...首先想到的是写一个应用,用一个ImageView,把这张照片附到ImageView上显示,如下面的demo。
  • Android设备上一张图片显示过程应用示例假如我们现在有一张这样的风景照 想在Android设备(比如一个小米pad)上显示出来。首先想到的是写一个应用,用一个ImageView,把这张照片附到ImageView上显示,如下面的...
  • 源代码地址:https://github.com/luoye123/Box根据昨天写的弹窗呢,完成了显示手机图片并选择的功能。 闲话不说了,上代码吧,因为代码有点,我这着重于说一部分!上一篇弹窗博客地址:...
  • 编写不易,如有转载,请声明出处: 梦回河口:GridView可以理解为显示多列的ListView,这次就利用GridView做一个仿桌面的案例。GridView和ListView的使用相同,都基本使用以下三步走:设置数据源,设置适配器,设置...
  • package com.example.viewpagertext; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android
  •   在Android中,常使用setOnClickListener方法来实现监听事件处理,但是还有一种方式是在xml文件中设置onClick属性,下面通过一个例子来说明两种方式如何实现。 创建一个新的Android项目,在布局文件中添加一个...
  • 今天有一个简单小需求,就是我们的ListView的数据列表,当没有数据时候,需要显示一张图片,当然这也符合用户的体验,1.布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...
  • 首先来看下面的效果
  • 转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38140505自从Gallery被谷歌废弃以后,Google推荐使用ViewPager和HorizontalScrollView来实现Gallery的效果。的确HorizontalScrollView可以实现...
  • 觉得博文有用,请点赞,请评论,请关注,谢谢!~ 老规矩,先上GIF动态图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧: MainActivity: ...import android.graphics.Bitmap...import android.os.
  • 2.将要显示图片img.png放到res/drawable-mdpi目录下 3.打开res/layout目录下的main.xml文件,用如下代码替换 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android
  • Android ImageView 不显示JPEG图片  今天在写一个小实例,ImageView在xml里面设置的是INVISIBLE,在代码里需要设置成setVisibility(View.VISIBLE),但图片没有显示出来,换成PNG或其它的JPEG格式的图片确可以正常...
  • Android图片选择器框架,支持多张,单张,拍照等返回选择的图片的uri,使用简单,效率高
  • scrollview嵌套一个imageview,显示一张很大的图片的时候,图片显示不出来,布局如下: android:id="@+id/scrollView" android:layout_width="wrap_content" android:layout_height="match_parent"> ...
1 2 3 4 5 ... 20
收藏数 238,468
精华内容 95,387