1

我正在尝试从文件位置选择多个图像。到目前为止,我已经设法选择了一张图片,但是我如何才能同时选择两张图片。

Intent intent =new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, STEP_4_REQUEST);

然后在以下onActivityResult(int requestCode, int resultCode, Intent data)方法中:

case STEP_4_REQUEST:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();

                Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
            } 
4

4 回答 4

2

尝试了许多替代方案,最简单和最强大的是:https ://github.com/luminousman/MultipleImagePick

于 2013-09-11T08:28:24.753 回答
2

为此,我创建了我的自定义画廊。

试试这个很有魅力

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.ijoomer.customviews.IjoomerButton;
import com.ijoomer.customviews.IjoomerCheckBox;
import com.ijoomer.src.R;

/**
 * This Class Contains All Method Related To IjoomerPhotoGalaryActivity.
 * 
 * @author tasol
 * 
 */
public class IjoomerPhotoGalaryActivity extends Activity {

    private ImageAdapter imageAdapter;

    private String[] arrPath;
    private boolean[] thumbnailsselection;
    private int ids[];
    private int count;


    /**
     * Overrides methods
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_gallery);

        final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
        final String orderBy = MediaStore.Images.Media._ID;
        @SuppressWarnings("deprecation")
        Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
        int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
        this.count = imagecursor.getCount();
        this.arrPath = new String[this.count];
        ids = new int[count];
        this.thumbnailsselection = new boolean[this.count];
        for (int i = 0; i < this.count; i++) {
            imagecursor.moveToPosition(i);
            ids[i] = imagecursor.getInt(image_column_index);
            int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
            arrPath[i] = imagecursor.getString(dataColumnIndex);
        }
        GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
        imageAdapter = new ImageAdapter();
        imagegrid.setAdapter(imageAdapter);
        imagecursor.close();

        final IjoomerButton selectBtn = (IjoomerButton) findViewById(R.id.selectBtn);
        selectBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                final int len = thumbnailsselection.length;
                int cnt = 0;
                String selectImages = "";
                for (int i = 0; i < len; i++) {
                    if (thumbnailsselection[i]) {
                        cnt++;
                        selectImages = selectImages + arrPath[i] + "|";
                    }
                }
                if (cnt == 0) {
                    Toast.makeText(getApplicationContext(), "Please select at least one image", Toast.LENGTH_LONG).show();
                } else {

                    Log.d("SelectedImages", selectImages);
                    Intent i = new Intent();
                    i.putExtra("data", selectImages);
                    setResult(Activity.RESULT_OK, i);
                    finish();
                }
            }
        });
    }
    @Override
    public void onBackPressed() {
        setResult(Activity.RESULT_CANCELED);
        super.onBackPressed();

    }

    /**
     * Class method
     */

    /**
     * This method used to set bitmap.
     * @param iv represented ImageView 
     * @param id represented id
     */

    private void setBitmap(final ImageView iv, final int id) {

        new AsyncTask<Void, Void, Bitmap>() {

            @Override
            protected Bitmap doInBackground(Void... params) {

                return MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
            }

            @Override
            protected void onPostExecute(Bitmap result) {
                super.onPostExecute(result);
                iv.setImageBitmap(result);
            }
        }.execute();
    }


    /**
     * List adapter
     * @author tasol
     */

    public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

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

        public int getCount() {
            return count;
        }

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.photo_gallery_item, null);
                holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
                holder.IjoomerCheckBox = (IjoomerCheckBox) convertView.findViewById(R.id.itemCheckBox);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.IjoomerCheckBox.setId(position);
            holder.imageview.setId(position);
            holder.IjoomerCheckBox.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    IjoomerCheckBox cb = (IjoomerCheckBox) v;
                    int id = cb.getId();
                    if (thumbnailsselection[id]) {
                        cb.setChecked(false);
                        thumbnailsselection[id] = false;
                    } else {
                        cb.setChecked(true);
                        thumbnailsselection[id] = true;
                    }
                }
            });
            holder.imageview.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    int id = holder.IjoomerCheckBox.getId();
                    if (thumbnailsselection[id]) {
                        holder.IjoomerCheckBox.setChecked(false);
                        thumbnailsselection[id] = false;
                    } else {
                        holder.IjoomerCheckBox.setChecked(true);
                        thumbnailsselection[id] = true;
                    }
                }
            });
            try {
                setBitmap(holder.imageview, ids[position]);
            } catch (Throwable e) {
            }
            holder.IjoomerCheckBox.setChecked(thumbnailsselection[position]);
            holder.id = position;
            return convertView;
        }
    }


    /**
     * Inner class
     * @author tasol
     */
    class ViewHolder {
        ImageView imageview;
        IjoomerCheckBox IjoomerCheckBox;
        int id;
    }

}

photo_gallery.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <Button android:id="@+id/selectBtn"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Select" android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:minWidth="200px" />
    <GridView android:id="@+id/PhoneImageGrid"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:numColumns="auto_fit" android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp" android:columnWidth="90dp"
        android:stretchMode="columnWidth" android:gravity="center"
        android:layout_above="@id/selectBtn" />
</RelativeLayout>

photo_gallery_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView android:id="@+id/thumbImage" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_centerInParent="true" 
        />
    <CheckBox android:id="@+id/itemCheckBox" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>
于 2013-09-11T04:15:18.493 回答
2

我自己得到了它:发布它,因为如果需要它可能会帮助其他人

我们应该告诉 android,当我们打开图库并选择共享按钮时,我的应用程序必须是共享选项之一,例如:

manifestfile:

<activity android:name=".selectedimages">
    <intent-filter >
        <action android:name="android.intent.action.SEND_MULTIPLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="image/*" />
    </intent-filter>
</activity>

选择我们的应用程序后,它将打开每个图像上带有复选框的图库以选择图像,在应用程序中处理选定的图像:

selectedimages.java 文件:

if (Intent.ACTION_SEND_MULTIPLE.equals(getIntent().getAction())
 && getIntent().hasExtra(Intent.EXTRA_STREAM)) {
 ArrayList<Parcelable> list =
        getIntent().getParcelableArrayListExtra(Intent.EXTRA_STREAM);
            for (Parcelable parcel : list) {
               Uri uri = (Uri) parcel;
               String sourcepath=getPath(uri);

               /// do things here with each image source path.
           }
            finish();
 }
 }

public  String getPath(Uri uri) {
  String[] projection = { MediaStore.Images.Media.DATA };
  Cursor cursor = managedQuery(uri, projection, null, null, null);
  startManagingCursor(cursor);
  int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  return cursor.getString(column_index);
  }

你也可以参考这个链接:

http://www.javacodegeeks.com/2012/10/android-select-multiple-photos-from-gallery.html

希望对你有帮助..!!!

于 2013-09-11T04:48:03.523 回答
0

嗨,下面的代码工作正常。

 Cursor imagecursor1 = managedQuery(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
    null, orderBy + " DESC");

   this.imageUrls = new ArrayList<String>();
  imageUrls.size();

   for (int i = 0; i < imagecursor1.getCount(); i++) {
   imagecursor1.moveToPosition(i);
   int dataColumnIndex = imagecursor1
     .getColumnIndex(MediaStore.Images.Media.DATA);
   imageUrls.add(imagecursor1.getString(dataColumnIndex));
  }

   options = new DisplayImageOptions.Builder()
  .showStubImage(R.drawable.stub_image)
  .showImageForEmptyUri(R.drawable.image_for_empty_url)
  .cacheInMemory().cacheOnDisc().build();

   imageAdapter = new ImageAdapter(this, imageUrls);

   gridView = (GridView) findViewById(R.id.PhoneImageGrid);
  gridView.setAdapter(imageAdapter);

你想要更多的澄清。 http://mylearnandroid.blogspot.in/2014/02/multiple-choose-custom-gallery.html

于 2014-05-09T04:14:56.773 回答