我想开始选择多张图片的意图。 例如,当意图引导您选择图库时,您需要长按并选择几个图像。我想获取此图像的URI。 我怎样才能做到这一点? 谢谢你的帮助
答案 0 :(得分:0)
没有默认方法可以做到这一点。 这是我在项目中为此创建的一项活动:
public class GalleryActivity extends AtionbarActivity {
// -----------------------------------------------------------------------
//
// Constants
//
// -----------------------------------------------------------------------
public static final String EXTRA_SELECTED_ITEMS = "EXTRA_SELECTED_ITEMS";
public static final String EXTRA_SELECT_MODE_SINGLE = "EXTRA_SELECT_MODE_SINGLE";
// -----------------------------------------------------------------------
//
// Fields
//
// -----------------------------------------------------------------------
private GridView mGridView;
private GalleryAdapter mAdapter;
private boolean mSelectSingle = false;
// -----------------------------------------------------------------------
//
// Methods
//
// -----------------------------------------------------------------------
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
mSelectSingle = getIntent().getBooleanExtra(EXTRA_SELECT_MODE_SINGLE,
false);
findViews();
initViews();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private void findViews() {
mGridView = (GridView) findViewById(R.id.activity_gallery_gridview);
}
private void initViews() {
mAdapter = new GalleryAdapter(this, R.layout.grid_item_gallery_image,
mSelectSingle);
mGridView.setAdapter(mAdapter);
mGridView.setOnItemClickListener(mImageClickListener);
}
private void updateGrid() {
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.LATITUDE,
MediaStore.Images.Media.LONGITUDE };
Cursor cursor = getContentResolver().query(uri, projection, null, null,
null);
ArrayList<GalleryImage> list = new ArrayList<GalleryImage>();
if (cursor.moveToFirst())
while (cursor.moveToNext()) {
GalleryImage image = new GalleryImage();
int columnIndex = cursor
.getColumnIndex(MediaStore.Images.Media.DATA);
image.path = cursor.getString(columnIndex);
columnIndex = cursor
.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN);
image.date = new Date(cursor.getLong(columnIndex));
columnIndex = cursor
.getColumnIndex(MediaStore.Images.Media.LATITUDE);
image.lat = cursor.getDouble(columnIndex);
columnIndex = cursor
.getColumnIndex(MediaStore.Images.Media.LONGITUDE);
image.lng = cursor.getDouble(columnIndex);
list.add(image);
}
mAdapter.setItems(list);
cursor.close();
}
@Override
public void onSaveInstanceState(Bundle outState) {
mAdapter.onSaveInstanceState(outState);
super.onSaveInstanceState(outState);
}
@Override
public void onRestoreInstanceState(@NotNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mAdapter.onRestoreInstanceState(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
updateGrid();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.gallery_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.menu_confirm:
Intent intent = new Intent();
intent.putExtra(EXTRA_SELECTED_ITEMS, mAdapter.getSelectedItems());
setResult(RESULT_OK, intent);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// -----------------------------------------------------------------------
//
// Listeners
//
// -----------------------------------------------------------------------
private OnItemClickListener mImageClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
mAdapter.applySelection(arg2);
}
};
}
我使用的适配器(您可以创建自己的适配器):
public class GalleryAdapter extends BaseAdapter {
// -----------------------------------------------------------------------
//
// Constants
//
// -----------------------------------------------------------------------
private static final String EXTRA_SELECTED_ITEMS = "EXTRA_SELECTED_ITEMS";
// -----------------------------------------------------------------------
//
// Fields
//
// -----------------------------------------------------------------------
private ArrayList<GalleryImage> mItems;
private ArrayList<GalleryImage> mSelectedItems;
private LayoutInflater mInflater;
private int mResId;
private boolean mSelectSingle;
// -----------------------------------------------------------------------
//
// Constructor
//
// -----------------------------------------------------------------------
public GalleryAdapter(Context context, int resource, boolean selectMode) {
mResId = resource;
mItems = new ArrayList<GalleryImage>();
mSelectedItems = new ArrayList<GalleryImage>();
mSelectSingle = selectMode;
mInflater = LayoutInflater.from(context);
}
// -----------------------------------------------------------------------
//
// Methods
//
// -----------------------------------------------------------------------
@Override
public View getView(int position, View convertView, ViewGroup parent) {
GalleryImage item = (GalleryImage) getItem(position);
if (convertView == null)
convertView = mInflater.inflate(mResId, parent, false);
ImageView thumbnail = (ImageView) ViewHolder.get(convertView,
R.id.grid_item_gallery_image);
String thumbTag = (String) thumbnail.getTag();
if (thumbTag == null || !thumbTag.equals(item.path))
ImageManager.displayImage("file:/" + item.path, thumbnail);
thumbnail.setTag(item.path);
ImageView selection = (ImageView) ViewHolder.get(convertView,
R.id.grid_item_gallery_selection);
selection.setVisibility(mSelectedItems.contains(item) ? View.VISIBLE
: View.GONE);
return convertView;
}
public void setItems(ArrayList<GalleryImage> items) {
mItems = items;
notifyDataSetChanged();
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Object getItem(int index) {
return mItems.get(index);
}
@Override
public long getItemId(int arg0) {
return 0;
}
public void applySelection(int position) {
GalleryImage image = mItems.get(position);
if (mSelectSingle)
mSelectedItems.clear();
if (!mSelectedItems.contains(image))
mSelectedItems.add(image);
else
mSelectedItems.remove(image);
notifyDataSetChanged();
}
public void onRestoreInstanceState(Bundle savedInstanceState) {
mSelectedItems = savedInstanceState
.getParcelableArrayList(EXTRA_SELECTED_ITEMS);
notifyDataSetChanged();
}
public void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList(EXTRA_SELECTED_ITEMS, mSelectedItems);
}
public ArrayList<GalleryImage> getSelectedItems() {
return mSelectedItems;
}
}
我用来存储图像数据的结构:
public class GalleryImage implements Parcelable {
public String path;
public Date date;
public double lat;
public double lng;
public GalleryImage() {
}
public GalleryImage(String path, Date date, double lat, double lng) {
this.path = path;
this.date = date;
this.lat = lat;
this.lng = lng;
}
public GalleryImage(Parcel in) {
this.path = in.readString();
this.date = new Date(in.readLong());
this.lat = in.readDouble();
this.lng = in.readDouble();
}
@Override
public boolean equals(Object o) {
return ((GalleryImage) o).path.equals(path);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(path);
dest.writeLong(date.getTime());
dest.writeDouble(lat);
dest.writeDouble(lng);
}
public static final Parcelable.Creator<GalleryImage> CREATOR = new Parcelable.Creator<GalleryImage>() {
public GalleryImage createFromParcel(Parcel in) {
return new GalleryImage(in);
}
public GalleryImage[] newArray(int size) {
return new GalleryImage[size];
}
};
}
您可以通过在EXTRA_SELECT_MODE_SINGLE
或true
中设置false
来指定是否需要多种/单一选择模式。这段代码将是一个非常好的起点。