Search code examples
androidimagegridviewandroid-gallery

I can't able to take my Images to new activity that are loaded from SD card


Image activity

public class ImageAdapter extends BaseAdapter {


private Context context;
Bitmap bm;

ArrayList<String>images = new ArrayList<String>();



//  Integer[] images;
void add(String path) {
    images.add(path);
}


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

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

@Override
public Object getItem(int position) {

    return images;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView = new ImageView(context);
    if (convertView == null) {
        //     imageView.setImageResource(images[position]);
        //   imageView.setImageBitmap(images);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setLayoutParams(new GridView.LayoutParams(240, 240));
        return imageView;
    } else {
        imageView = (ImageView) convertView;

    }
     bm = decodeSampledBitmapFromUri(images.get(position), 220, 220);
    imageView.setImageBitmap(bm);
return imageView;
}

private 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;
}
}

Main Activity

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent i= new Intent(getApplicationContext(), FullImageActivity.class);
            i.putExtra("id", position);
            startActivity(i);

        }
    });

FullImageActivity

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_image);

    Intent i=getIntent();

    int position= i.getExtras().getInt("id");
    ImageAdapter imageAdapter= new ImageAdapter(this);





    ImageView imageView=(ImageView)findViewById(R.id.imageView);

     imageView.setImageBitmap(imageAdapter.bm); \\here I have to show my images that are read from Storage


}

I can't able to pass it properly because my arraylist are string. I tried many ways that I know but that not work properly. I tried from internet also but not working. I can't show my images through fullimage activity. I read images from SD card so It not showing in full view. Thanks in advance


Solution

  • Your code is not executing properly because it is not setup correctly. First thing's first, consider using Picasso or Glide or UniversalImageLoader for loading your images into the placeholders to avoid making a clunky user experience scrolling through images.

    Secondly, passing the position to the second activity is not your goal. You are trying to pass the image. So pass the image lol,

    First on your getItem() 
    

    you should return the individual image based on position images.get(position)

    rather then return images.

    Secondly in your click handler

    Either pass it as an extra like

    intent.addExtra(myAdapter.getItem(position))
    

    or if you have the list already just do

    intent.addExtra(myList.get(position))
    
    or simply cheat with ActivityTwo.imageToShow = myList.get(position)
    startActivityTwo
    

    anyone of these will work fine.