检索android中的所有图像目录

时间:2017-09-03 07:18:39

标签: android android-adapter android-file subdirectory android-gridlayout

我试过这个来获取目录并从中加载图像但是加载图像速度非常慢,当选择相机时它会在一段时间后崩溃...

< --------从中获取目录和图像的代码------->

public static ArrayList<String> getImageBuckets(Context mContext){
    ArrayList<String> buckets = new ArrayList<>();
    Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    String [] projection = {MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media.DATA};

    Cursor cursor = mContext.getContentResolver().query(uri, projection, null, null, null);
    if(cursor != null){
        File file;
        while (cursor.moveToNext()){
            String bucketPath = cursor.getString(cursor.getColumnIndex(projection[0]));
            String fisrtImage = cursor.getString(cursor.getColumnIndex(projection[1]));
            file = new File(fisrtImage);
            if (file.exists() && !buckets.contains(bucketPath)) {
                buckets.add(bucketPath);
            }
        }
        cursor.close();
    }
    return buckets;
}


public static ArrayList<String> getImagesByBucket(Context mContext , @NonNull String bucketPath){

    Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    String [] projection = {MediaStore.Images.Media.DATA};
    String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME+" =?";
    String orderBy = MediaStore.Images.Media.DATE_ADDED+" DESC";

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

    Cursor cursor = mContext.getContentResolver().query(uri, projection, selection,new String[]{bucketPath}, orderBy);

    if(cursor != null){
        File file;
        while (cursor.moveToNext()){
            String path = cursor.getString(cursor.getColumnIndex(projection[0]));
            file = new File(path);
            if (file.exists() && !images.contains(path)) {
                images.add(path);
            }
        }
        cursor.close();
    }
    return images;
}

&LT; ----------这是选择目录的适配器和方法---------&gt;

        <--------------------Adapter------------------>


public class GridImageAdapter extends ArrayAdapter<String> {
    private Context mContext;
    private LayoutInflater mInflater;
    private int layoutResource;
    private String mAppend;
    private ArrayList<String> imgURLs;

    public GridImageAdapter(Context context, int layoutResource, String append, ArrayList<String> imgURLs) {
        super(context, layoutResource, imgURLs);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mContext = context;
        this.layoutResource = layoutResource;
        mAppend = append;
        this.imgURLs = imgURLs;
    }

    @Override
    public int getCount() {
        return imgURLs.size();
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(layoutResource, parent, false);
            holder = new ViewHolder();
            holder.mProgressBar = (ProgressBar) convertView.findViewById(R.id.gridImageProgressbar);
            holder.image = (SquareImageView) convertView.findViewById(R.id.gridImageView);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final String imgURL = getItem(position);

        Picasso.with(mContext).load(mAppend + imgURL).error(R.drawable.ic_person).into(holder.image, new com.squareup.picasso.Callback() {
            @Override
            public void onSuccess() {
                if (holder.mProgressBar != null) {
                    Picasso.with(mContext).load(mAppend + imgURL).into(holder.image);
                    holder.mProgressBar.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError() {
                if (holder.mProgressBar != null) {
                    Picasso.with(mContext).load(mAppend + imgURL).into(holder.image);
                    holder.mProgressBar.setVisibility(View.GONE);
                }

            }
        });

        return convertView;
    }

    private static class ViewHolder {
        SquareImageView image;
        ProgressBar mProgressBar;
    }
}

&LT; --------------设置网格视图的方法-----------&gt;

private void Directory() {

    directories = FileSearch.getImageBuckets(getActivity());

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity()
            ,android.R.layout.simple_spinner_item ,directories);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    directorySpinner.setAdapter(adapter);

    directorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
            Log.d(TAG, "onItemSelected: selected" + directories.get(position));

            setGridView(directories.get(position));
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}

private void setGridView(String selectedDir){
    Log.d(TAG, "setGridView: Dir chosen");
    final ArrayList<String> imageURL = FileSearch.getImagesByBucket(getActivity() ,selectedDir);

    int gridWidth = getResources().getDisplayMetrics().widthPixels;
    int imageWidth = gridWidth/NUM_GRID_COLOUMN;
    gridView.setColumnWidth(imageWidth);

    GridImageAdapter adapter = new GridImageAdapter(getActivity() , R.layout.grid_image_layout ,append , imageURL);
    gridView.setAdapter(adapter);
}

0 个答案:

没有答案