我想从图库中选择图片,并试图在GridView中显示,但它是不是在GridView的设置,下面是我的片断code,谁能告诉我是什么问题,我的code?在此先感谢
公共类MainActivity延伸活动{
私有静态诠释RESULT_LOAD_IMAGE = 1;
私人ImageView的ImageView的;
私人乌里selectedImage;
私人诠释参数:columnIndex;
私人GridView控件GridView控件;
私人字符串picturePath;
私人ImageView的imageView11;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
按钮buttonLoadImage =(按钮)findViewById(R.id.buttonLoadPicture);
GridView控件=(GridView控件)findViewById(R.id.gridview);
gridView.setAdapter(新ImageAdapter(本));
gridView.setOnItemClickListener(新OnItemClickListener()
{
公共无效onItemClick(适配器视图<>父,
视图V,INT位置,长ID)
{
Toast.makeText(getBaseContext(),
PIC+(位置+ 1)+选择,
Toast.LENGTH_SHORT).show();
}
});
buttonLoadImage.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(查看为arg0){
意向意图=新的意图(Intent.ACTION_GET_CONTENT);
intent.setType(图像/ *);
startActivityForResult(意向,1);
}
});
}
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,因此code,数据);
如果(要求code == RESULT_LOAD_IMAGE和放大器;&安培;结果code == RESULT_OK和放大器;&安培;!NULL =数据){
selectedImage = data.getData();
的String [] filePathColumn = {MediaStore.Images.Media.DATA};
光标光标= getContentResolver()查询(selectedImage,
filePathColumn,NULL,NULL,NULL);
cursor.moveToFirst();
ImageView的=(ImageView的)findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.de codeFILE(picturePath));
参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
字符串picturePath = cursor.getString(参数:columnIndex);
objImageAdapter.addToList(picturePath);
cursor.close();
}
}
公共类ImageAdapter扩展了BaseAdapter
{
私人上下文的背景下;
ArrayList的<字符串> ArrayList的;
公共ImageAdapter(上下文C)
{
上下文= C;
}
// ---返回图像的数量---
公众诠释getCount将(){
返回arrayList.size();
}
// ---返回一个项目的ID ---
公共对象的getItem(INT位置){
返回的位置;
}
无效addToList(字符串strPath中)
{
this.arrayList.add(strPath的);
this.notifyDataSetChanged();
}
众长getItemId(INT位置){
返回的位置;
}
//在这个数组,你必须保存所有图片的路径是要在baseapater显示,并且必须是全球性的baseapater访问
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
ImageView的ImageView的;
如果(convertView == NULL){
ImageView的=新ImageView的(上下文);
imageView.setLayoutParams(新GridView.LayoutParams(185,185));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5,5,5,5);
} 其他 {
ImageView的=(ImageView的)convertView;
}
字符串路径= arrayList.get(位置);
位图MYBITMAP = BitmapFactory.de codeFILE(路径);
imageView.setImageBitmap(MYBITMAP);
返回ImageView的;
}
}
activity_main.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< GridView控件
机器人:ID =@ + ID / GridView控件
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:columnWidth中=90dp
机器人:为numColumns =auto_fit
机器人:verticalSpacing =10dp
机器人:horizontalSpacing =10dp
机器人:stretchMode =columnWidth中
机器人:重力=中心
/>
< ImageView的
机器人:ID =@ + ID / imgView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1>< / ImageView的>
<按钮
机器人:ID =@ + ID / buttonLoadPicture
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0
机器人:文本=加载图片
机器人:layout_gravity =中心>< /按钮>
< / LinearLayout中>
解决方案
我想从图库中选择图片,并试图在显示它 gridview的,但它未在gridview的设定
执行以下变化ImageAdapter类:
1 返回 ArrayList的大小
从 getCount将
方法:
公众诠释getCount将(){
返回arrayList.size();
}
2 创建于当前适配器的数据源增加新的选择图像的方法:
无效addToList(字符串strPath中)
{
this.arrayList.add(strPath的);
this.notifyDataSetChanged();
}
现在在 onActivityResult
呼叫 addToList
方法来显示在GridView中所选图像:
参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
字符串picturePath = cursor.getString(参数:columnIndex);
objImageAdapter.addToList(picturePath);
cursor.close();
I am trying to select image from gallery and trying to display it in gridview but it is not setting in gridview,following is my snippet code,can anyone tell me what is the issue with my code? Thanks in advance
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
private ImageView imageView;
private Uri selectedImage;
private int columnIndex;
private GridView gridView;
private String picturePath;
private ImageView imageView11;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
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)
{
Toast.makeText(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
}
});
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
objImageAdapter.addToList(picturePath);
cursor.close();
}
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
ArrayList<String> arrayList;
public ImageAdapter(Context c)
{
context = c;
}
//---returns the number of images---
public int getCount() {
return arrayList.size();
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
void addToList(String strPath)
{
this.arrayList.add(strPath);
this.notifyDataSetChanged();
}
public long getItemId(int position) {
return position;
}
//In this array you have to store all images path which is you want to display in baseapater and must be global to access in baseapater
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
String path = arrayList.get(position);
Bitmap myBitmap = BitmapFactory.decodeFile(path);
imageView.setImageBitmap(myBitmap);
return imageView;
}
}
activity_main.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">
<GridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
<ImageView
android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"></ImageView>
<Button
android:id="@+id/buttonLoadPicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Load Picture"
android:layout_gravity="center"></Button>
</LinearLayout>
解决方案
I am trying to select image from gallery and trying to display it in gridview but it is not setting in gridview
Do following Changes in ImageAdapter class :
1. Return size of arrayList
from getCount
method:
public int getCount() {
return arrayList.size();
}
2. Create a method for adding new selected image in current adapter data-source :
void addToList(String strPath)
{
this.arrayList.add(strPath);
this.notifyDataSetChanged();
}
Now in onActivityResult
call addToList
method to show selected image in GridView:
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
objImageAdapter.addToList(picturePath);
cursor.close();