Search code examples
androidandroid-arrayadapterandroid-gridview

How to enlarge a picture in Gridview when it is clicked using a new activity?


so I have an activity that inflates a GridView, and I want it to be clickable, and upon click, it opens a new activity, shows the whole image, along with some texts that I've written to in another activity. Note that it is confirmed that the txt file which was written to is written successfully, and it is in the SD Card.

Here is the code for the GridView activity

public class GridView extends AppCompatActivity {

android.widget.GridView gv;
ArrayList<File> list,list2;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery);

    File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyCameraApp");
    File dir2 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyCameraApp");
    list = imageReader(dir);
    list2 = locationReader(dir2);


    gv = (android.widget.GridView) findViewById(R.id.gridView);
    gv.setAdapter(new GridAdapter());

    gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(GridView.this, ViewImage.class);
            intent.putExtra("MyImage", list.get(position));
            intent.putExtra("MyLocation", list2.get(position));
            startActivity(intent);
            //startActivity(new Intent(getApplicationContext(), ViewImage.class).putExtra("img",list.get(position)));
        }
    });
}

public void BackToMain(View view) {
    Intent backToMain = new Intent(this, MainActivity.class);
    startActivity(backToMain);
}

class GridAdapter extends BaseAdapter{

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = getLayoutInflater().inflate(R.layout.simple_grid, parent, false);
        ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);
        iv.setImageURI(Uri.parse(getItem(position).toString()));

        return convertView;
    }
}


ArrayList<File> imageReader(File root){
    ArrayList<File> a = new ArrayList<>();

    File[] files = root.listFiles();
    for(int i = 0; i < files.length; i++){
        if(files[i].isDirectory()){

        }
        else
        {
            if(files[i].getName().endsWith(".jpg")){
                a.add(files[i]);
            }
        }
    }
    return a;
}
ArrayList<File> locationReader(File root){
    ArrayList<File> a = new ArrayList<>();

    File[] files = root.listFiles();
    for(int i = 0; i < files.length; i++){
        if(files[i].isDirectory()){

        }
        else
        {
            if(files[i].getName().endsWith(".txt")){
                a.add(files[i]);
            }
        }
    }
    return a;
}

}

Here is the activity that's called by GridView, that is supposedly show the Image plus open a file that I've written to earlier, and show its content (A String).

public class ViewImage extends AppCompatActivity {

ImageView imageView;
TextView textView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_image);

 //   int image = getIntent().getExtras().getInt("MyImage", R.drawable.common_google_signin_btn_text_light);
    int image = getIntent().getIntExtra("MyImage", R.drawable.cast_ic_notification_0);
    ImageView imageView = (ImageView) findViewById(R.id.imageView2);
    imageView.setImageResource(image);

    Intent calledActivity = getIntent();

    String pos = calledActivity.getExtras().getString("MyLocation");
    textView = (TextView) findViewById(R.id.text_location);
    textView.setText(pos);

}

public void BackToCamera(View view) {
    Intent intent = new Intent(ViewImage.this, MainActivity.class);
    startActivity(intent);
}
}

Right now, all I see is R.drawable.cast_ic_notification_0 being the picture, as opposed to the GridView's image. Also, the TextView is empty, I used the debugger and it showed that

String pos = calledActivity.getExtras().getString("MyLocation");

pos is null. Please help!


Solution

  • list.get(position) is a File and you're trying to read it as getExtras().getInt()

    I think you should pass the file path as String:

    intent.putExtra("MyImage", list.get(position).getAbsolutePath());
    

    and receive it:

    File imgFile = new  File(getIntent().getStringExtra("MyImage"));
    if(imgFile.exists()){
        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        ImageView imageView = (ImageView) findViewById(R.id.imageView2);
        imageView.setImageBitmap(myBitmap);
    }