1

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>

3 Answers3

1

How did you set position in imageView.setImageResource(), in SetImageResource if you set Gridview cell position than it's can't display image because it's in position variable only cell index are store like 0,1,2. You need to set your bitmap or Resource ID incest of "positon" in your Baseadapter getView method.

1

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();
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • it says objImageAdapter cannot be resolved.. and i edited my answer is it right? –  Apr 18 '15 at 09:49
  • @chris: add `ImageAdapter objImageAdapter;` before onCreate and in onCreate method change `gridView.setAdapter(new ImageAdapter(this));` to `objImageAdapter= new ImageAdapter(this); gridView.setAdapter(objImageAdapter);` – ρяσѕρєя K Apr 18 '15 at 09:55
  • Caused by: java.lang.NullPointerException..at com.example.multiple_gridviews.MainActivity$ImageAdapter.getCount(MainActivity.java:113).. and line number 113 is..return arrayList.size(); –  Apr 18 '15 at 09:58
  • @chris: ok change `ArrayList arrayList;` to `ArrayList arrayList=new ArrayList();` – ρяσѕρєя K Apr 18 '15 at 10:00
  • is it possible to open gallery and select multiple image and show it in grid? –  Apr 18 '15 at 10:23
  • @chris: see following post [Get/pick an image from Android's built-in Gallery app programmatically](http://stackoverflow.com/questions/2169649/get-pick-an-image-from-androids-built-in-gallery-app-programmatically) probably help – ρяσѕρєя K Apr 18 '15 at 10:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75580/discussion-between-chris-and--k). –  Apr 18 '15 at 12:05
  • action_multiple_send allows to send images and msgs etc..so how to open custom gallery with folders –  Apr 18 '15 at 12:47
  • i need to open gallery with folders..i also followed this http://mylearnandroid.blogspot.in/2014/02/multiple-choose-custom-gallery.html.............but it brings all the images instead of folder vise –  Apr 18 '15 at 13:19
  • is it possible can you check? http://stackoverflow.com/questions/29739949/the-method-setrequestpropertystring-string-in-the-type-urlconnection-is-not-a –  Apr 20 '15 at 06:16
0

First you store all your Gallery images path in global arrayList and than give to getView method position to your array and get single path for particular adapter cell after that just do it like belove code

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;
}
Nikunj Patel
  • 303
  • 2
  • 11
  • Accepted my answer if you this is help you. – Nikunj Patel Apr 18 '15 at 07:32
  • 1
    Ok select any single image full path from your Gallery and give that path directly and than check it. If than also didn't work than just check your image resolution like 2000 or above than you have to resize your image resolution because high resolution did not show in imageview – Nikunj Patel Apr 18 '15 at 07:36
  • i dont want to show in imageview i need to display imageview in gridview –  Apr 18 '15 at 07:42
  • is it because i gave width of imageview fiillparent? –  Apr 18 '15 at 07:52
  • no metter what ever your imageview size but when your image resolution is too high than your actual phone resolution than you have to resize than only it's display, Actually image is set on imageview but can't display because of resolution problem and even it's can't throw any exception so you can't get any error when you run or debug. – Nikunj Patel Apr 18 '15 at 07:56
  • yup so what i am trying is first i will open gallery,then select image and display it in my gridview,and again i wil browse second image and display it in gridview..so then in my gridview two image should be shown..but i am not getting –  Apr 18 '15 at 08:01
  • Maybe problem in your path which you give to your imageview or maybe your adapter not refreshing properly you have to debug in your adapter to get call or not – Nikunj Patel Apr 18 '15 at 08:10