从图库中选择图像后未在 gridview 中设置
after selection of image from gallery is not set in gridview
我正在尝试 select 图片库中的图像并尝试在 gridview 中显示它,但它没有在 gridview 中设置,以下是我的代码片段,谁能告诉我我的代码有什么问题?提前致谢
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>
你是如何在 imageView.setImageResource() 中设置位置的,如果你在 SetImageResource 中设置了 Gridview 单元格位置,那么它无法显示图像,因为它位于位置变量中,只有单元格索引存储为 0,1,2 .
您需要在您的 Baseadapter getView 方法中设置 "positon" 的位图或资源 ID。
首先,您将所有图库图像路径存储在全局 arrayList 中,然后将 getView 方法位置提供给您的数组,然后为特定的适配器单元获取单一路径,就像亲爱的代码一样
ArrayList<String> arrayList; //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;
}
I am trying to select image from gallery and trying to display it in
gridview but it is not setting in gridview
在 ImageAdapter 中进行以下更改 class:
1. Return 来自 getCount
方法的 arrayList
大小:
public int getCount() {
return arrayList.size();
}
2. 创建在当前适配器数据源中添加新选定图像的方法:
void addToList(String strPath)
{
this.arrayList.add(strPath);
this.notifyDataSetChanged();
}
现在 onActivityResult
调用 addToList
方法在 GridView 中显示所选图像:
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
objImageAdapter.addToList(picturePath);
cursor.close();
我正在尝试 select 图片库中的图像并尝试在 gridview 中显示它,但它没有在 gridview 中设置,以下是我的代码片段,谁能告诉我我的代码有什么问题?提前致谢
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>
你是如何在 imageView.setImageResource() 中设置位置的,如果你在 SetImageResource 中设置了 Gridview 单元格位置,那么它无法显示图像,因为它位于位置变量中,只有单元格索引存储为 0,1,2 . 您需要在您的 Baseadapter getView 方法中设置 "positon" 的位图或资源 ID。
首先,您将所有图库图像路径存储在全局 arrayList 中,然后将 getView 方法位置提供给您的数组,然后为特定的适配器单元获取单一路径,就像亲爱的代码一样
ArrayList<String> arrayList; //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;
}
I am trying to select image from gallery and trying to display it in gridview but it is not setting in gridview
在 ImageAdapter 中进行以下更改 class:
1. Return 来自 getCount
方法的 arrayList
大小:
public int getCount() {
return arrayList.size();
}
2. 创建在当前适配器数据源中添加新选定图像的方法:
void addToList(String strPath)
{
this.arrayList.add(strPath);
this.notifyDataSetChanged();
}
现在 onActivityResult
调用 addToList
方法在 GridView 中显示所选图像:
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
objImageAdapter.addToList(picturePath);
cursor.close();