Android - 保存IMG并以新布局显示

时间:2015-12-17 15:26:07

标签: android android-intent android-camera

我是android的初学者,我正在尝试制作一个带有第一个版面的照片(使用意图)的应用程序,并将其显示在第二个版面上。

如果我不更改布局,我可以显示图片。但我希望拍摄的照片能够以全新的布局展示。我目前正在使用它,试图将它带到我的共享布局,但这不起作用。

    if(requestCode == CAM_REQUEST){
        setContentView(R.layout.share);
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        imgTakenPhoto.setImageBitmap(thumbnail);
    }

我按照我在youtube上找到的教程。 这是我的mainAcitivty代码:

(进口)

public class MainActivity extends AppCompatActivity {
    ImageButton btnTakePhoto;
    ImageView imgTakenPhoto;
    private static final int CAM_REQUEST = 1313;

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

        btnTakePhoto = (ImageButton) findViewById(R.id.buttonFoto);
        imgTakenPhoto = (ImageView) findViewById(R.id.genomenFoto);

        btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == CAM_REQUEST){
            setContentView(R.layout.share); //Change layout & display img in it
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            imgTakenPhoto.setImageBitmap(thumbnail);
        }
    }

    class btnTakePhotoClicker implements Button.OnClickListener {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAM_REQUEST);
        }
    }
    //Share layout -- back button - Go back to first layout
    public void ibBackToPhotograph(View v) {
        setContentView(R.layout.activity_main);
    }
}

1 个答案:

答案 0 :(得分:0)

我想你在shareactivity_main ImageView id="@+id/genomenFoto"

的布局上

因此,在使用setContentView方法更改布局后,您必须从新布局中检索ImageView。在{2}布局上具有相同ID的2 ImageView罕见不同Object

这样:

if(requestCode == CAM_REQUEST){
    setContentView(R.layout.share);
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    imgTakenPhoto = (ImageView) findViewById(R.id.genomenFoto);
    imgTakenPhoto.setImageBitmap(thumbnail);
}