如何在新活动中打开照片?

时间:2017-08-08 07:18:35

标签: java android

当您单击相机打开的按钮并拍摄新照片时,我进行了编码。我希望该图片在新的Activity上变成ImageView。所以我创建了新活动并在其上放置了一个ImageView:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.amy.teacherfilesapp.Upload">

        <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:scaleType="centerCrop"
        />


</android.support.constraint.ConstraintLayout>

然后在我放的主要活动上(这整个事情适用于btn2所以你可以忽略btn1&amp; btn3,谢谢):`package com.example.amy.teacherfilesapp;

import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {

    Button btn1;
    Button btn2;
    Button btn3;
    ImageView imgTakenPic;
    private static final int CAM_REQUEST=1313;


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


        btn2 = (Button) findViewById(R.id.drawer_two);
        imgTakenPic = (ImageView)findViewById(R.id.imageView);
        btn2.setOnClickListener(new btnTakePhotoClicker());

        btn1 = (Button)findViewById(R.id.drawer_one);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent openCabinet = new Intent(MainActivity.this,MyCabinet.class);
                startActivity(openCabinet);
            }
        });

        btn2 =(Button)findViewById(R.id.drawer_two);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent upload = new Intent("android.media.action.IMAGE_CAPTURE");
                startActivity(upload);
            }
        });

        btn3 = (Button)findViewById(R.id.drawer_three);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent settings = new Intent(MainActivity.this, Settings.class);
                startActivity(settings);
            }
        });





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

        if(requestCode == CAM_REQUEST){
            Bitmap bitmap = (Bitmap) data.getExtras().get("data");
            imgTakenPic.setImageBitmap(bitmap);
        }
    }

    class btnTakePhotoClicker implements  Button.OnClickListener{

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent,CAM_REQUEST);
        }
    }
}

`

这不起作用,因为它没有在我能看到的任何地方显示图像。

如果你能帮助我,我将非常感激。感谢。

1 个答案:

答案 0 :(得分:1)

像这样使用;

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

    if(requestCode == CAM_REQUEST){
        Bitmap photo = (Bitmap) data.getExtras().get("data");
            /* Passing BITMAP to the Second Activity */
            Intent IntentCamera = new Intent(this, Second.class);
            IntentCamera.putExtra("BitmapImage", photo);
            startActivity(IntentCamera);
    }
}
第二个活动中的

;

 Intent intent_camera = getIntent();
Bitmap camera_img_bitmap = (Bitmap) intent_camera
        .getParcelableExtra("BitmapImage");
if (camera_img_bitmap != null) {
    img_to_be_zoomed.setImageBitmap(camera_img_bitmap);
}