在gridView中显示从一个活动到另一个活动的照片

时间:2015-09-13 10:43:59

标签: android gridview

这是我将照片发送到下一个活动的代码。现在,我如何在接收活动中显示GridView中拍摄的照片。

发送:

button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(!imageTaken){
                Toast.makeText(getActivity(), "You've not taken any image!", Toast.LENGTH_SHORT).show();
                return;
            }
            Intent iSecond=new Intent(getActivity(),ShowImage.class);
            iSecond.putExtra("pictureUri", imageUri.toString());
            startActivity(iSecond);
        }
    });

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "Launching Camera", Toast.LENGTH_SHORT).show();

            Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
            if(imagesFolder.mkdirs()){
                Log.d(TAG, "The directory is created");
            } else {
                Log.d(TAG, "Failed or already exists");
            }
            File image = new File(imagesFolder, "image_001.jpg");
            try {
                if(image.createNewFile()){
                    Log.d(TAG, "The file is created");
                } else {
                    Log.d(TAG, "The file already exists");
                }
            } catch (IOException e) {
                Log.e(TAG, "createNewFile Failed");
                e.printStackTrace();
            }
            imageUri = Uri.fromFile(image);
            imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(imageIntent, REQUEST_CODE_FROM_CAMERA);
        }
    });
    return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_FROM_CAMERA && resultCode == Activity.RESULT_OK) {
        imageTaken = true;
        Toast.makeText(getActivity(), "The image was taken",Toast.LENGTH_SHORT).show();
    }
}

接收活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show);
    grid = (GridView) findViewById(R.id.gridviewimg);

    grid.setImageURI(Uri.parse(getIntent().getStringExtra("pictureUri")));
}

接收活动的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<RelativeLayout
    android:id="@+id/RelativeGridLayout"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" >

    <GridView
        android:id="@+id/gridviewimg"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:numColumns="2"
        android:scrollbarStyle="outsideInset"
        android:smoothScrollbar="true"
        android:verticalSpacing="10dp"
        android:paddingBottom="50dp"
        android:paddingTop="10dp"
        />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:layout_alignBottom="@+id/RelativeGridLayout"
    >


</RelativeLayout>

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您无法像往常一样将图像直接设置为GridView。您必须创建自定义适配器。

对于自定义适配器,您需要首先创建GridView项目的布局(在您的情况下是和ImageView)。

之后,将(数量)图像URI或URL传递给Adapter,并将适配器设置为GridView。

可能this链接可以为您提供提示。