从图库中获取所有照片并在网格视图中显示它

时间:2016-05-10 09:04:29

标签: android

我想从画廊中取出所有照片并将其显示在网格视图中..我得到的所有照片但问题是它不会像画廊那样顺利移动挂机请给我解决方案如果任何人有我的代码是下方。

    public class getImageFromGallery extends AsyncTask<String,Void,ArrayList<String>> {

        @Override
        protected ArrayList<String> doInBackground(String... strings) {
            Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            String[] projection = {MediaStore.Images.ImageColumns.DATA};
            Cursor c = null;
            SortedSet<String> dirList = new TreeSet<String>();
            final ArrayList<String> resultIAV = new ArrayList<String>();

            String[] directories = null;

            if (u != null)
            {
                String sortOrder ="_id asc limit 1";
                c = managedQuery(u, projection, null, null, null);
            }

            if ((c != null) && (c.moveToFirst()))
            {
                do
                {
                    String tempDir = c.getString(0);
                    tempDir = tempDir.substring(0, tempDir.lastIndexOf("/"));
                    try{
                        dirList.add(tempDir);
                    }
                    catch(Exception e)
                    {
                    }
                }
                while (c.moveToNext());
                directories = new String[dirList.size()];
                dirList.toArray(directories);

            }

            for(int i=0;i<dirList.size();i++)
            {
                File imageDir = new File(directories[i]);
                File[] imageList = imageDir.listFiles();
                if(imageList == null)
                    continue;
                for (File imagePath : imageList) {
                    try {

                        if(imagePath.isDirectory())
                        {
                            imageList = imagePath.listFiles();

                        }
                        if ( imagePath.getName().contains(".jpg")|| imagePath.getName().contains(".JPG")
                                || imagePath.getName().contains(".jpeg")|| imagePath.getName().contains(".JPEG")
                                || imagePath.getName().contains(".png") || imagePath.getName().contains(".PNG")
                                || imagePath.getName().contains(".gif") || imagePath.getName().contains(".GIF")
                                || imagePath.getName().contains(".bmp") || imagePath.getName().contains(".BMP")
                                )
                        {



                            String path= imagePath.getAbsolutePath();
                            resultIAV.add(path);

                        }
                    }
                    //  }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

           ShareMoment.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    shareMomentAdapter  = new ShareMomentAdapter(ShareMoment.this,resultIAV);
                    sharemomentList.setAdapter(shareMomentAdapter);
                }
            });

            return resultIAV;

        }
    }

public class ShareMomentAdapter extends RecyclerView.Adapter<ShareMomentAdapter.ShareMomentViewHolder> {
    private Context context;
    private ArrayList<String> shareMomentList;
    ByteArrayOutputStream bytearrayoutputstream;

    public ShareMomentAdapter(Context context, ArrayList<String> shareMomentList) {
        this.shareMomentList = shareMomentList;
//        shareMomentList = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            shareMomentList.add("" + i);
//        }
        this.context = context;
        bytearrayoutputstream = new ByteArrayOutputStream();
    }

    @Override
    public ShareMomentAdapter.ShareMomentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_sharemoments, parent, false);


        return new ShareMomentViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final ShareMomentAdapter.ShareMomentViewHolder holder, int position) {
        Bitmap yourSelectedImage = BitmapFactory.decodeFile(shareMomentList.get(position));
        final Bitmap resizedImage = Bitmap.createScaledBitmap(yourSelectedImage, 150, 150, true);

        Glide.with(context).load(shareMomentList.get(position)).diskCacheStrategy(DiskCacheStrategy.RESULT).crossFade().into(holder.shareMomentImage);
    }

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

    public class ShareMomentViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.shareMomentImage)
        ImageView shareMomentImage;
        //        @Bind(R.id.nameTextview)
//        TextView nameTextview;
//        @Bind(R.id.descriptionTextView)
//        TextView descriptionTextView;
        private View view;

        public ShareMomentViewHolder(View view) {
            super(view);
            this.view = view;
            ButterKnife.bind(this, view);
        }
    }


    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
                matrix, false);

        return resizedBitmap;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试使用LoadMoreView而不是一次加载所有图像。一次加载10-20个图像,然后在用户向下滚动时加载更多图像。这将节省大量内存。