如何使用新活动点击Gridview中的图片放大?

时间:2016-02-07 22:16:21

标签: android android-arrayadapter android-gridview

所以我有一个让GridView膨胀的活动,我希望它可以点击,点击后,它会打开一个新的活动,显示整个图像,以及我写在另一个文本中的一些文本活动。请注意,已确认写入的txt文件已成功写入,并且位于SD卡中。

以下是GridView活动的代码

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

}

以下是GridView调用的活动,据说可以显示Image并打开我之前写过的文件,并显示其内容(字符串)。

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

现在,我看到的只是R.drawable.cast_ic_notification_0是图片,而不是GridView的图像。此外,TextView是空的,我使用调试器,它显示

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

pos为null。请帮忙!

1 个答案:

答案 0 :(得分:1)

func2()是一个文件,你试图把它读作getExtras()。getInt()

我认为您应该将文件路径作为String传递:

list.get(position)

并收到它:

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