从Android手机获取图像和视频到自定义图库

时间:2013-12-20 00:03:42

标签: android gridview gallery

我正在尝试创建一个自定义图库,允许用户从其Android设备上包含的所有照片和视频中进行选择。我知道如何创建一个仅包含照片和视频的图库,但如果我想将两者结合起来,我该怎么做呢?

我认为问题归结为我如何创建光标。为了选择所有视频,我以这种方式创建了光标:

String[] videoParams = {MediaStore.Video.Media._ID,
        MediaStore.Video.Media.DATA,
        MediaStore.Video.Media.DATE_TAKEN,
        MediaStore.Video.Thumbnails.DATA};

videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoParams, null, null, null);

如果我想查询所有媒体文件,而不仅仅是视频,我该怎么办?

这是我尝试过的,基于:Custom Gallery with Images and Videos in android to select multiple items

    String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
    String jpg_mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("jpg");
    String png_mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("png");
    String mp4_mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("mp4");
    String[] selectionArgs = new String[]{jpg_mimeType, png_mimeType, mp4_mimeType};

    mediaCursor = getContentResolver().query(MediaStore.Files.getContentUri("internal"), null, selectionMimeType, selectionArgs, MediaStore.Files.FileColumns.DATE_ADDED);

这给了我错误 java.lang.IllegalArgumentException:无法绑定索引3处的参数,因为索引超出范围。该语句在android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)中有1个参数

也许我的方法是完全错误的,但我找不到使用图像和视频的自定义安卓图库的任何示例,这对我来说很奇怪,因为这似乎是创建常见的事情。

以下是我的所有代码,以防它有用:

public class GridViewCompatActivity extends Activity {

    GridViewCompat gridView;

    private static final String TAG = "GridViewCompatActivity"; 

    Cursor videocursor;
    Cursor mediaCursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_view_compat);

        gridView = (GridViewCompat) findViewById(R.id.gridView1);

        // NOTE: We are using setChoiceMode, as I said, its a drop-in replacement
        gridView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        gridView.setAdapter(new ImageAdapter(getApplicationContext()));

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> view, View arg1, int pos, long id) {
                // We need to invalidate all views on 4.x versions
                GridViewCompat gridView = (GridViewCompat) view;
                gridView.invalidateViews();
            }

        });

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

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                SparseBooleanArray checkArray;
                checkArray = gridView.getCheckedItemPositions();

                String selectedPos = "Selected positions: ";
                int count = checkArray.size();
                for (int i = 0; i < count; i++) {
                    if (checkArray.valueAt(i))
                        selectedPos += checkArray.keyAt(i) + ",";
                }

                Intent intent = new Intent();
                intent.putExtra("result", selectedPos);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
    }

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            Log.d(TAG, "number of media: " + Integer.toString(MediaStore.Files.FileColumns.DATA.length()));
            int mediaParams = MediaStore.MediaColumns.DATA.length();
            return mediaParams;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new grid view item for each item referenced by the Adapter
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            CheckBox checkBox;
            if (convertView == null) {
                LayoutInflater layoutInflater = LayoutInflater.from(mContext);
                convertView = layoutInflater.inflate(R.layout.grid_view_item, parent, false);
            }
            imageView = (ImageView) convertView.findViewById(R.id.imageView1);
            checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);

            GridViewCompat gvc = (GridViewCompat) parent;
            if (gvc.getChoiceMode() == ListView.CHOICE_MODE_MULTIPLE) {
                SparseBooleanArray checkArray;
                checkArray = gvc.getCheckedItemPositions();

                checkBox.setChecked(false);
                if (checkArray != null) {
                    if (checkArray.get(position)) {
                        checkBox.setChecked(true);
                    }
                }

            }
            // imageView.setImageResource(mThumbIds[position]);
            Bitmap bmThumbnail;
            Log.d(TAG, "position: " + position);
            mediaCursor = getContentResolver().query(MediaStore.Files.getContentUri("internal"), null, selectionMimeType, selectionArgs, MediaStore.Files.FileColumns.DATE_ADDED);
            Log.d(TAG, Integer.toString(mediaCursor.getCount()));

            for (int i = 0; i < mediaCursor.getCount(); i++){
                mediaCursor.moveToPosition(i);
                Boolean isVideo = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA)).length() > 0;
                Log.d(TAG, "isVideo: " + isVideo);
                String mediaPath = ""; 
                if(isVideo){
                    mediaPath = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
                    video_paths.add(mediaPath);    
                }else{
                    mediaPath = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Images.Media.DATA));
                    video_paths.add(mediaPath);
                }
                Log.d(TAG, "mediaPath: " +mediaPath);
            }     

            mediaCursor.moveToPosition(position);
            String video_path = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
            Log.d(TAG, "video_path: " + video_path);
            imageView.setImageBitmap(ThumbnailUtils.createVideoThumbnail(video_path, Thumbnails.MICRO_KIND));

            return convertView;
        }

        ArrayList<String> video_paths = new ArrayList<String>();

        String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
        String jpg_mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("jpg");
        String png_mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("png");
        String mp4_mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("mp4");
        String[] selectionArgs = new String[]{jpg_mimeType, png_mimeType, mp4_mimeType};

//        String[] videoParams = {MediaStore.Video.Media._ID,
//                MediaStore.Video.Media.DATA,
//                MediaStore.Video.Media.DATE_TAKEN,
//                MediaStore.Video.Thumbnails.DATA};
//        }
}
}

2 个答案:

答案 0 :(得分:4)

public class GalleryFragment extends Fragment
{
    private int count;
    private Bitmap[] thumbnails;
    private boolean[] thumbnailsselection;
    private String[] arrPath;
    private int[] typeMedia;
    private ImageAdapter imageAdapter;

    @SuppressLint("NewApi") @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.gallery_gridview, container, false);         

        String[] columns = { MediaStore.Files.FileColumns._ID, 
                MediaStore.Files.FileColumns.DATA,
                MediaStore.Files.FileColumns.DATE_ADDED,
                MediaStore.Files.FileColumns.MEDIA_TYPE,
                MediaStore.Files.FileColumns.MIME_TYPE,
                MediaStore.Files.FileColumns.TITLE,
                };      
        String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                 + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE 
                 + " OR "
                 + MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                 + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;
        final String orderBy = MediaStore.Files.FileColumns.DATE_ADDED;     
        Uri queryUri = MediaStore.Files.getContentUri("external");

        @SuppressWarnings("deprecation")
        Cursor imagecursor = getActivity().managedQuery(queryUri,
            columns,
            selection,
            null, // Selection args (none).
            MediaStore.Files.FileColumns.DATE_ADDED + " DESC" // Sort order.
          );

        int image_column_index = imagecursor.getColumnIndex(MediaStore.Files.FileColumns._ID);
        this.count = imagecursor.getCount();
        this.thumbnails = new Bitmap[this.count];
        this.arrPath = new String[this.count];
        this.typeMedia = new int[this.count];
        this.thumbnailsselection = new boolean[this.count];     
        for (int i = 0; i < this.count; i++) {
            imagecursor.moveToPosition(i);
            int id = imagecursor.getInt(image_column_index);            
            int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);            
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 4;
            bmOptions.inPurgeable = true;           
            int type = imagecursor.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE);         
            int t = imagecursor.getInt(type);   
            if(t == 1)
            thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
                    getActivity().getContentResolver(), id,
                    MediaStore.Images.Thumbnails.MINI_KIND, bmOptions);
            else if(t == 3)
                thumbnails[i] = MediaStore.Video.Thumbnails.getThumbnail(
                        getActivity().getContentResolver(), id,
                        MediaStore.Video.Thumbnails.MINI_KIND, bmOptions);

            arrPath[i]= imagecursor.getString(dataColumnIndex);
            typeMedia[i] = imagecursor.getInt(type);
        }

        GridView imagegrid = (GridView) v.findViewById(R.id.PhoneImageGrid);        
        Button reSizeGallery = (Button) v.findViewById(R.id.reSizeGallery); 

        imageAdapter = new ImageAdapter();  
        imagegrid.setAdapter(imageAdapter);
        imagecursor.close();    

        reSizeGallery.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View v) {
                ChatViewerAdapter.ScreenResize(getActivity());              
            }
        });

        return v;//super.onCreateView(inflater, container, savedInstanceState);
    }

    public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;   

        public ImageAdapter() {
            mInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public int getCount() {
            return count;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        @SuppressLint("NewApi") public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;          

            Display display = getActivity().getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int width = size.x;
            int height = size.y;            

            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(
                        R.layout.gallery_view, null);
                holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
                holder.videoICON = (ImageView) convertView.findViewById(R.id.videoICON);
                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.imageview.getLayoutParams().height = height/6;
            holder.imageview.getLayoutParams().width = width/4;

            holder.imageview.setId(position);

            if(typeMedia[position] == 1)
                holder.videoICON.setVisibility(View.GONE);
            else if(typeMedia[position] == 3)
                holder.videoICON.setVisibility(View.VISIBLE);

            holder.imageview.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    int id = v.getId();                 

                    Display display = ((WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
                    int height = display.getHeight();
                    final int height_half = (int) (height/2.5);

                    RelativeLayout fragment_layout = (RelativeLayout) getActivity().findViewById(R.id.fragment_gallery);
                    fragment_layout.setVisibility(View.VISIBLE);
                    fragment_layout.getLayoutParams().height = height_half;

                    GalleryImageChooseFragment f_img_choose =new GalleryImageChooseFragment();  
                    FragmentManager fm = getFragmentManager();
                    FragmentTransaction ft = fm.beginTransaction();
                    Bundle args = new Bundle();
                    args.putString("PATH", arrPath[id]);
                    f_img_choose.setArguments(args);
                    ft.show(f_img_choose);      
                    ft.replace(R.id.fragment_tattle, f_img_choose);
                    ft.addToBackStack("f_img_choose");
                    ft.commit();
                }
            });
            holder.imageview.setImageBitmap(thumbnails[position]);
            holder.id = position;
            return convertView;
        }
    }

    class ViewHolder {
        ImageView imageview;
        ImageView videoICON;
        int id;
    }
}

答案 1 :(得分:1)

在此处下载源代码表单(https://angular.io/docs/ts/latest/api/core/index/ApplicationRef-class.html)。

 public void fn_video() {

        int int_position = 0;
        Uri uri;
        Cursor cursor;
        int column_index_data, column_index_folder_name,column_id,thum;

        String absolutePathOfImage = null;
        uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

        String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Video.Media.BUCKET_DISPLAY_NAME,MediaStore.Video.Media._ID,MediaStore.Video.Thumbnails.DATA};

        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.Video.Media.BUCKET_DISPLAY_NAME);
        column_id = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
        thum = cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA);

        while (cursor.moveToNext()) {
            absolutePathOfImage = cursor.getString(column_index_data);
            Log.e("Column", absolutePathOfImage);
            Log.e("Folder", cursor.getString(column_index_folder_name));
            Log.e("column_id", cursor.getString(column_id));
            Log.e("thum", cursor.getString(thum));

            Model_Video obj_model = new Model_Video();
            obj_model.setBoolean_selected(false);
            obj_model.setStr_path(absolutePathOfImage);
            obj_model.setStr_thumb(cursor.getString(thum));

            al_video.add(obj_model);

        }

}