使用ViewPager的电话库

时间:2017-05-04 01:10:31

标签: android android-studio android-viewpager gallery

嗨,我是android的新手,

我来自这个帖子并遵循了如何使用Viewpager How to do Gallery Images swap Left To right bydefault

好像我正在加载来自drawable的图像:

private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};

如何从手机图库中获取图片?

1 个答案:

答案 0 :(得分:0)

  

以下功能从图库中获取所有图像列表

    private ArrayList<String> getAllShownImagesPath(Activity activity) {
        Uri uri;
        Cursor cursor;
        int column_index_data, column_index_folder_name;
        ArrayList<String> listOfAllImages = new ArrayList<String>();
        String absolutePathOfImage = null;
        uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        String[] projection = { MediaColumns.DATA,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME };

        cursor = activity.getContentResolver().query(uri, projection, null,
                null, null);

        column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        column_index_folder_name = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
        while (cursor.moveToNext()) {
            absolutePathOfImage = cursor.getString(column_index_data);

            listOfAllImages.add(absolutePathOfImage);
        }
        return listOfAllImages;
    }