显示图库中的多个图像选择

时间:2019-03-11 05:34:56

标签: android

有人可以告诉我如何允许图库从图库意图中选择多个图像。当前,此代码打开图库。但是我不知道如何允许画廊选择多个图像

 public Intent getPickImageChooserIntent() {

    Uri outputFileUri = getCaptureImageOutputUri();

    List<Intent> allIntents = new ArrayList<>();
    PackageManager packageManager = getPackageManager();

    Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam) {
        Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(res.activityInfo.packageName);

        if (outputFileUri != null) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        }
        allIntents.add(intent);


    }

    Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
    galleryIntent.setType("*/*");
    List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0);
    for (ResolveInfo res : listGallery) {
        Intent intent = new Intent(galleryIntent);
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(res.activityInfo.packageName);
        allIntents.add(intent);

    }

    Intent mainIntent = allIntents.get(allIntents.size() - 1);
    for (Intent intent : allIntents) {
        if (intent.getComponent().getClassName().equals("com.example.healthpassport.MainActivity")) {
            mainIntent = intent;
            break;
        }
    }
    allIntents.remove(mainIntent);

    Intent chooserIntent = Intent.createChooser(mainIntent, "Select source");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()]));
    return chooserIntent;
}

2 个答案:

答案 0 :(得分:0)

您好,您可以从以下查询中获取图像,也可以通过将查询“ MediaStore.Images.Media.EXTERNAL”更改为“ MediaStore.videos.Media.EXTERNAL”,仅同时从该查询获取视频。

 //----------------------------------------------------------------------------------------------//
private void populateImagesFromGallery()
{
   //if permission is not granted than return
    if (!mayRequestGalleryImages())
    {
        return;
    }
    final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};
    final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
    ArrayList<Media> imageUrls = new ArrayList<>();



        imageUrls.addAll(loadPhotosAndVideosFromNativeGallery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, orderBy, true));


    //ahsan adapter
    initializeRecyclerView(imageUrls);
}

这是加载所有图像的另一个功能

 //----------------------------------------------------------------------------------------------//
private ArrayList<Media> loadPhotosAndVideosFromNativeGallery(Uri tableName, String[] columns, String orderBy, boolean isImage)
{
    //final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
    //final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
    Cursor imagecursor = managedQuery(
            tableName, columns, null,
            null, orderBy + " DESC");

    ArrayList<Media> imageUrl = new ArrayList<>();

    for (int i = 0; i < imagecursor.getCount(); i++)
    {
        imagecursor.moveToPosition(i);
        int dataColumnIndex;
        Log.e("cursor", imagecursor.toString());
        if (isImage)
        {
            dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
        }
        else
        {
            dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Video.Media.DATA);
        }

        imageUrl.add(new Media(imagecursor.getString(dataColumnIndex), imagecursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN), isImage));

    }


    return imageUrl;
}

调用适配器的初始化函数,将该函数传递给您的数组以填充

 //----------------------------------------------------------------------------------------------//
private void initializeRecyclerView(ArrayList<Media> imageUrls)
{
    imageAdapter = new ImageAdapter(this, imageUrls);

    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 3);
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.addItemDecoration(new ItemOffsetDecoration(this, R.dimen.item_offset));
    recyclerView.setAdapter(imageAdapter);

    //it will restrict to onbind view holder to call again and again and call only for the item count of the array
    recyclerView.setItemViewCacheSize(imageAdapter.getItemCount());
}

适配器

 public class ImageAdapter extends         RecyclerView.Adapter<ImageAdapter.MyViewHolder>
  {

private ArrayList<Media> mImagesList;
private Context mContext;
private SparseBooleanArray mSparseBooleanArray;

private List<Media> selectedImages ;


public void setOnEventListener(OnEventListener listener) {
    mOnEventListener = listener;
}


public interface OnEventListener {
    void onSelect();
    // or void onEvent(); as per your need
}

private OnEventListener mOnEventListener;

public ImageAdapter(Context context, ArrayList<Media> imageList)
{
    mContext = context;
    mSparseBooleanArray = new SparseBooleanArray();
    mImagesList = imageList;
    selectedImages = new ArrayList<>();


}


public void selectAll()
{
    for ( int i=0; i < mImagesList.size(); i++) {
        mSparseBooleanArray.put((Integer) i, true);

    }

    notifyDataSetChanged();
    mOnEventListener.onSelect();

}

//--------------------------------------------------------------------------------------//
public List<Media> getCheckedItems()
{
    ArrayList<Media> mTempArry = new ArrayList<Media>();



    return selectedImages;
}

//--------------------------------------------------------------------------------------//
@Override
public long getItemId(int position)
{
    return position;
}

CompoundButton.OnCheckedChangeListener mCheckedChangeListener = new CompoundButton.OnCheckedChangeListener()
{

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {

    }
};

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{

    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.row_multiphoto_item, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder,  int position)
{

    final String imageUrl = String.valueOf(mImagesList.get(position).getFilePath());

    final int pos = position;


    Glide.with(mContext)
            .load("file://" + imageUrl)
            .centerCrop()
            .placeholder(R.drawable.ic_launcher_background)
            .error(R.drawable.ic_launcher_background)
            .into(holder.imageView);

    if (selectedImages.contains(mImagesList.get(pos)))
    {
        holder.ic_Selected.setImageResource(R.drawable.ic_select_item);
        holder.itemView.setAlpha(0.5f);
    }
    else
    {
        holder.ic_Selected.setImageResource(R.drawable.ic_unselect_item);
        holder.itemView.setAlpha(1f);
    }

 //   holder.ic_Selected.setImageResource(selectedImages.contains(mImagesList.get(pos)) ? R.drawable.ic_select_item : R.drawable.ic_unselect_item);

    holder.itemView.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            holder.itemClicked(pos);
        }
    });

}

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

public class MyViewHolder extends RecyclerView.ViewHolder
{
    ImageView imageView;
    public ImageView ic_Selected;

    public MyViewHolder(View view)
    {
        super(view);


        imageView =  view.findViewById(R.id.imageView1);
        ic_Selected =  view.findViewById(R.id.ic_Selected);
    }

    public void itemClicked(int pathPosition)
    {

        // if selected item is contain that path than remove from list and if not than add in the list
        if (selectedImages.contains(mImagesList.get(pathPosition)))
        {
            selectedImages.remove(mImagesList.get(pathPosition));

        }
        else
        {
            selectedImages.add(mImagesList.get(pathPosition));

        }

        mOnEventListener.onSelect();

        notifyDataSetChanged();
    }
} 

}

答案 1 :(得分:0)

如果需要,该库对于显示和获取图像非常有用。它也用于获取gif。git