Question

I'm new to android and have been working with a tutorial which loads all images from a directory on the SD card. I have the tutorial running but would like to add some code to the click listener which will load a full size version of the thumbnail in the gridview image adapter.

public class PicturesFragment extends Fragment {

public class ImageAdapter extends BaseAdapter {

private Context mContext;
ArrayList<String> itemList = new ArrayList<String>();

public ImageAdapter(Context c) {
    mContext = c;
}

void add(String path) {
    itemList.add(path);
}

@Override
public int getCount() {
    return itemList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return itemList.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) { // if it's not recycled, initialize some
                                // attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(225, 225));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(1, 1, 1, 1);
    } else {
        imageView = (ImageView) convertView;
    }

    Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 220,
            220);

    imageView.setImageBitmap(bm);
    return imageView;
}

public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,
        int reqHeight) {

    Bitmap bm = null;
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(path, options);

    return bm;
}

public int calculateInSampleSize(

BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height
                    / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }

    return inSampleSize;
}

}

ImageAdapter myImageAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.pictures_fragment, container, false);

GridView gridview = (GridView) rootView.findViewById(R.id.gridview);
myImageAdapter = new ImageAdapter(getActivity());
gridview.setAdapter(myImageAdapter);

String ExternalStorageDirectoryPath = Environment
        .getExternalStorageDirectory().getAbsolutePath();

String targetPath = ExternalStorageDirectoryPath + "/myfilepath/Images";

Toast.makeText(getActivity().getApplicationContext(), targetPath, Toast.LENGTH_LONG)
        .show();
File targetDirector = new File(targetPath);

File[] files = targetDirector.listFiles();
for (File file : files) {
    myImageAdapter.add(file.getAbsolutePath());
}

gridview.setOnItemClickListener(myOnItemClickListener);


Button camerabtn = (Button) rootView.findViewById(R.id.camerabtn);

camerabtn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(v.getContext(), CameraActivity.class);
        startActivityForResult(intent, 0);
    }
});

  return rootView;
}

Here's the click listener where I believe I should be implementing the code to solve this problem. I think I should be launching another activity (FullImageActivity.class) which just contains a layout with an imageview and pass the image which has been clicked to this new activity , but I have little know how to go about this

OnItemClickListener myOnItemClickListener = new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    String prompt = (String) parent.getItemAtPosition(position);
    Toast.makeText(getActivity().getApplicationContext(), prompt, Toast.LENGTH_LONG)
            .show();

    //Intent i = new Intent(getActivity().getApplicationContext(), FullImageActivity.class);

   // startActivity(i);

   }
  };
}

Any help would be greatly appreciated.

Was it helpful?

Solution

Yes, change the onItemClick like this..

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
    String path = (String) myImageAdapter.getItem(position);

    Intent intent = new Intent(this, FullImageActivity.class);
    intent.putExtra("IMAGEPATH",path);
    startActivity(intent);

    }

And in FullImageActivity have a layout to show the full image from the path. you can get the path in the FullImageActivity onCreate()

 public class FullImageActivity extends ActionBarActivity{

@Override
public void onCreate(Bundle bundle){

    super.onCreate(bundle);
    setContentView(R.layout.image_layout);
    String path = getIntent().getStringExtra("IMAGEPATH");
    // load the image from the path and set it to the imageview in layout 

}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top