如何在android的Gridview中实现Image Gallery?
Posted
技术标签:
【中文标题】如何在android的Gridview中实现Image Gallery?【英文标题】:How to implement Image Gallery in Gridview in android? 【发布时间】:2011-10-14 21:15:43 【问题描述】:我有一个要求,我想用 GridView 实现一个图片库。 我尝试在 android 开发者网站上使用 Hello gallery。但是 GridView 不起作用。
【问题讨论】:
你想添加一个Gallery对象或者把照片放到GridView中?,查看GridView教程:Hello GridView Tutorial - Android Developers 【参考方案1】:将此 XML 用于布局:gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:layout_ android:background="@drawable/bg_child">
<FrameLayout android:id="@+id/FrameLayout01"
android:layout_ android:layout_>
<FrameLayout android:id="@+id/LinearLayout01"
android:layout_gravity="top" android:layout_ android:layout_>
<TextView android:id="@+id/TextView01"
android:layout_ android:layout_ android:textStyle="bold" android:layout_gravity="center_vertical" android:layout_marginLeft="30dp" android:gravity="center_vertical" android:drawableLeft="@drawable/photo_frame" android:textColor="@color/grey" android:text="@string/photogallery_txt"></TextView>
<Button android:layout_gravity="right" android:id="@+id/btnMoreInfo" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:textStyle="bold" android:background="@drawable/my_child_button" android:layout_ android:layout_ android:text="@string/moreinfo_txt"></Button>
</FrameLayout>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview" android:layout_
android:layout_ android:columnWidth="90dp"
android:numColumns="auto_fit" android:verticalSpacing="10dp"
android:horizontalSpacing="10dp" android:stretchMode="columnWidth"
android:gravity="center" android:layout_gravity="bottom"
android:layout_marginTop="50dp"></GridView>
</FrameLayout>
</LinearLayout>
活动文件 GalleryPage.java
public class GalleryPage extends Activity
// private Integer[] mImageIds = R.drawable.splash, R.drawable.splash,
// R.drawable.splash, R.drawable.splash, R.drawable.splash,
// R.drawable.splash, R.drawable.splash;
private static Uri[] mUrls = null;
private static String[] strUrls = null;
private String[] mNames = null;
private GridView gridview = null;
private Cursor cc = null;
private Button btnMoreInfo = null;
private ProgressDialog myProgressDialog = null;
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
CommonFunctions.setLanguage(getBaseContext());
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.gallery);
btnMoreInfo = (Button) findViewById(R.id.btnMoreInfo);
// It have to be matched with the directory in SDCard
cc = this.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,
null);
// File[] files=f.listFiles();
if (cc != null)
myProgressDialog = new ProgressDialog(GalleryPage.this);
myProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
myProgressDialog.setMessage(getResources().getString(R.string.pls_wait_txt));
//myProgressDialog.setIcon(R.drawable.blind);
myProgressDialog.show();
new Thread()
public void run()
try
cc.moveToFirst();
mUrls = new Uri[cc.getCount()];
strUrls = new String[cc.getCount()];
mNames = new String[cc.getCount()];
for (int i = 0; i < cc.getCount(); i++)
cc.moveToPosition(i);
mUrls[i] = Uri.parse(cc.getString(1));
strUrls[i] = cc.getString(1);
mNames[i] = cc.getString(3);
//Log.e("mNames[i]",mNames[i]+":"+cc.getColumnCount()+ " : " +cc.getString(3));
catch (Exception e)
myProgressDialog.dismiss();
.start();
gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
Intent i = new Intent(GalleryPage.this, BigImage.class);
Log.e("intent : ", ""+position);
i.putExtra("imgUrls", strUrls);
i.putExtra("position", position);
startActivity(i);
);
btnMoreInfo.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Intent i = new Intent(GalleryPage.this, ChildLogin.class);
startActivity(i);
);
/**
* This class loads the image gallery in grid view.
*
*/
public class ImageAdapter extends BaseAdapter
private Context mContext;
public ImageAdapter(Context c)
mContext = c;
public int getCount()
return cc.getCount();
public Object getItem(int position)
return null;
public long getItemId(int position)
return 0;
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent)
View v = convertView;
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.galchild, null);
try
ImageView imageView = (ImageView) v.findViewById(R.id.ImageView01);
//imageView.setScaleType(ImageView.ScaleType.FIT_XY);
// imageView.setPadding(8, 8, 8, 8);
Bitmap bmp = decodeURI(mUrls[position].getPath());
//BitmapFactory.decodeFile(mUrls[position].getPath());
imageView.setImageBitmap(bmp);
//bmp.
TextView txtName = (TextView) v.findViewById(R.id.TextView01);
txtName.setText(mNames[position]);
catch (Exception e)
return v;
@Override
protected void onStart()
// TODO Auto-generated method stub
super.onStart();
FlurryAgent.onStartSession(this, "***");
// @Override
// protected void onStop()
// TODO Auto-generated method stub
// super.onStop();
// FlurryAgent.onEndSession(this);
//
/**
* This method is to scale down the image
*/
public Bitmap decodeURI(String filePath)
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Only scale if we need to
// (16384 buffer for img processing)
Boolean scaleByHeight = Math.abs(options.outHeight - 100) >= Math.abs(options.outWidth - 100);
if(options.outHeight * options.outWidth * 2 >= 16384)
// Load, scaling to smallest power of 2 that'll get it <= desired dimensions
double sampleSize = scaleByHeight
? options.outHeight / 100
: options.outWidth / 100;
options.inSampleSize =
(int)Math.pow(2d, Math.floor(
Math.log(sampleSize)/Math.log(2d)));
// Do the actual decoding
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[512];
Bitmap output = BitmapFactory.decodeFile(filePath, options);
return output;
【讨论】:
太多瓦砾,-1 这个 有没有办法只获取 .gif 文件?【参考方案2】:看到这个hello-gridview。如需更多参考,另请参阅
gallery-into-grid-style-menu image-gallery-with-checkbox-in-grid-to-select-multiple/ add-capture-button-and-update-gallery/我认为以上链接对您非常有用。请记住,在发布问题之前,您首先要查看android developer。这将提供有关 Android 的所有大部分信息。如果您仍然遇到任何问题,请告诉我
【讨论】:
【参考方案3】:本教程Hello Grid View 提供了一个很好的使用gridview 创建图片库的基本示例,但是图片来自drawable 资源,您需要根据需要对其进行扩展。
【讨论】:
【参考方案4】:看起来你想要自定义画廊,这需要很长时间,
我建议您为您的工作获取Custom Camera Gallery library。
您将根据需要在网格视图中获得照片/视频。
【讨论】:
【参考方案5】:private static final int SELECT_PHOTO = 100;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.imageView2);
public void pickAImage(View view)
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode)
case SELECT_PHOTO:
if (resultCode == RESULT_OK)
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try
imageStream = getContentResolver().openInputStream(selectedImage);
catch (FileNotFoundException e)
e.printStackTrace();
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
image.setImageURI(selectedImage);// To display selected image in image view
【讨论】:
以上是关于如何在android的Gridview中实现Image Gallery?的主要内容,如果未能解决你的问题,请参考以下文章
如何在没有选择按钮的情况下在 GridView 中实现全行选择?
如何在TabBarView中实现SingleChildScrollView(带有水平列表视图和垂直gridview)
android ListView 嵌套GridView 子Item点击加载Activity