【问题标题】:how to get all the images from in built gallery to my application...?如何从内置图库中获取所有图像到我的应用程序...?
【发布时间】:2011-10-21 13:53:41
【问题描述】:

我想从内置的画廊活动中开始进行多选。

我尝试过this,,但它只选择了一张图片。

提前谢谢..!

【问题讨论】:

    标签: android android-activity android-gallery


    【解决方案1】:

    您必须创建自己的带有复选框的图像列表来选择多个图像文件。

    使用下面的代码获取画廊的光标并创建一个列表适配器,它将遍历光标并给出图像。此图像可用于在带有复选框的列表视图中显示。

    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    

    【讨论】:

    • 为此,您需要获取图像 URI,然后为该 URI 的光标创建适配器并创建图像列表视图。请参考:coderzheaven.com/2011/03/…
    • 我们正在打开画廊进行选择,并且一次只能选择一个。
    • 但我不想为单选打开图库。有没有什么办法没有开始活动,我们应该得到所有的图像。
    • 不,这是一个自定义图像列表,您可以使用复选框从中选择多个图像。只需 google 一下,您就会发现如何创建图像列表。
    【解决方案2】:

    从这里下载源代码 (Get all images from gallery in android programmatically)

    public ArrayList<Model_images> fn_imagespath() {
            al_images.clear();
    
            int int_position = 0;
            Uri uri;
            Cursor cursor;
            int column_index_data, column_index_folder_name;
    
            String absolutePathOfImage = null;
            uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    
            String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
    
            final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
            cursor = getApplicationContext().getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
    
            column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
            while (cursor.moveToNext()) {
                absolutePathOfImage = cursor.getString(column_index_data);
                Log.e("Column", absolutePathOfImage);
                Log.e("Folder", cursor.getString(column_index_folder_name));
    
                for (int i = 0; i < al_images.size(); i++) {
                    if (al_images.get(i).getStr_folder().equals(cursor.getString(column_index_folder_name))) {
                        boolean_folder = true;
                        int_position = i;
                        break;
                    } else {
                        boolean_folder = false;
                    }
                }
    
    
                if (boolean_folder) {
    
                    ArrayList<String> al_path = new ArrayList<>();
                    al_path.addAll(al_images.get(int_position).getAl_imagepath());
                    al_path.add(absolutePathOfImage);
                    al_images.get(int_position).setAl_imagepath(al_path);
    
                } else {
                    ArrayList<String> al_path = new ArrayList<>();
                    al_path.add(absolutePathOfImage);
                    Model_images obj_model = new Model_images();
                    obj_model.setStr_folder(cursor.getString(column_index_folder_name));
                    obj_model.setAl_imagepath(al_path);
    
                    al_images.add(obj_model);
    
    
                }
    
    
            }
    
    
            for (int i = 0; i < al_images.size(); i++) {
                Log.e("FOLDER", al_images.get(i).getStr_folder());
                for (int j = 0; j < al_images.get(i).getAl_imagepath().size(); j++) {
                    Log.e("FILE", al_images.get(i).getAl_imagepath().get(j));
                }
            }
            obj_adapter = new Adapter_PhotosFolder(getApplicationContext(),al_images);
            gv_folder.setAdapter(obj_adapter);
            return al_images;
        }
    

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 2018-06-08
      相关资源
      最近更新 更多