【问题标题】:How to Make a Photo Taken Open Up in a New Activity? [duplicate]如何在新活动中打开拍摄的照片? [复制]
【发布时间】:2017-08-08 15:22:21
【问题描述】:

当您单击相机打开并拍摄新照片的按钮时,我进行了编码。我希望那张图片在新的 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 和 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);
        }
    }
}

`

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

如果您能帮助我,我将不胜感激。谢谢。

【问题讨论】:

  • 点击照片并取回照片的代码在哪里。你是如何把你的图片从那个活动转移到这里的。
  • 看看这个场景的示例代码的答案:stackoverflow.com/a/5991757/232530
  • 请不要重复提问。只需使用您拥有的任何新信息、您尝试过的任何新代码或任何已发布答案不起作用的解释来编辑您的原始帖子,就会将它推到活动队列的顶部。

标签: java android


【解决方案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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 2016-09-21
    • 2011-03-30
    • 1970-01-01
    相关资源
    最近更新 更多