GridViewで画像ギャラリーを実装したいという要件があります。 Android開発者向けWebサイトで Hello gallery を使用してみましたが、GridViewが機能しません。
この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_width="fill_parent"
Android:layout_height="fill_parent" Android:background="@drawable/bg_child">
<FrameLayout Android:id="@+id/FrameLayout01"
Android:layout_width="fill_parent" Android:layout_height="fill_parent">
<FrameLayout Android:id="@+id/LinearLayout01"
Android:layout_gravity="top" Android:layout_height="50dp" Android:layout_width="fill_parent">
<TextView Android:id="@+id/TextView01"
Android:layout_width="wrap_content" Android:layout_height="wrap_content" 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_width="100dp" Android:layout_height="40dp" Android:text="@string/moreinfo_txt"></Button>
</FrameLayout>
<GridView xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:id="@+id/gridview" Android:layout_width="fill_parent"
Android:layout_height="fill_parent" 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;
}
}
これを見てください hello-gridview 。詳細については、こちらもご覧ください
上記のリンクは非常に役立つと思います。質問を投稿する前に、まずチェックしてください Android開発者 。これにより、Androidに関するほとんどすべての情報が提供されます。それでも問題が解決しない場合はお知らせください
このチュートリアル Helloグリッドビュー は、グリッドビューを使用してイメージギャラリーを作成するための優れた基本的な例を提供しますが、画像は描画可能なリソースからのものであり、要件に応じて拡張する必要があります。
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
}
}
}
}