Android Studio:在Android摄像头中拍照后如何用照片替换图像按钮

Android Studio:在Android摄像头中拍照后如何用照片替换图像按钮,android,android-camera,Android,Android Camera,有人能帮我解决我遇到的问题吗? 我想制作一个活动,在其中我有一个制作照片按钮(imageButton\u add\u Playerd\u image),按下后,我的应用程序会打开相机让我拍照(仅一张照片)。 制作完照片后,我想将其发送到此活动,并将“制作照片”按钮更改为用户制作的照片(但保留“制作照片”按钮活动,以便在照片不够好的情况下,可以将照片替换为其他照片)。 我在*.java文件中的代码如下: <!-- begin snippet --> import androi



有人能帮我解决我遇到的问题吗?

我想制作一个活动,在其中我有一个制作照片按钮(imageButton\u add\u Playerd\u image),按下后,我的应用程序会打开相机让我拍照(仅一张照片)。

制作完照片后,我想将其发送到此活动,并将“制作照片”按钮更改为用户制作的照片(但保留“制作照片”按钮活动,以便在照片不够好的情况下,可以将照片替换为其他照片)。

我在*.java文件中的代码如下:

<!-- begin snippet -->

    import android.app.Activity;
    import android.content.Intent;
    import android.provider.MediaStore;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ImageButton;
    import android.widget.RatingBar;
    import android.widget.Toast;

    import static ???.MainActivity.REQUEST_IMAGE_CAPTURE;

    public class AddPlayground extends AppCompatActivity {

      EditText playGroundName;
      RatingBar rate;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        setContentView(R.layout.activity_add_playground);
        playGroundName = (EditText) findViewById(R.id.editText_playground_name);
        rate = (RatingBar) findViewById(R.id.ratingBar);
        Double message = intent.getDoubleExtra(MainActivity.EXTRA_MESSAGE, 0);
        Toast.makeText(this, String.valueOf(message), Toast.LENGTH_SHORT).show();
      }

      public void addPhoto(View view) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
          }
      }

    }

<!-- end snippet -->
<!-- begin snippet -->
<?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="???.AddPlayground">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical" android:layout_width="368dp"
        android:layout_height="495dp"
        android:weightSum="1"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">
        <EditText
            android:id="@+id/editText_playground_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:hint="Name of playground" />
        <ImageButton
            android:id="@+id/imageButton_add_playground_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.52"
            android:onClick="addPhoto"
            app:srcCompat="@drawable/ic_menu_camera" />
        <RatingBar
            android:id="@+id/ratingBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="5"
            android:visibility="visible" />
        <EditText
            android:id="@+id/editText_playground_comment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textMultiLine"
            android:hint="Playground description" />
        <Button
            android:id="@+id/button_add_playground"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="addNewPlayground"
            android:text="Add" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>
<!-- end snippet -->

导入android.app.Activity;
导入android.content.Intent;
导入android.provider.MediaStore;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.EditText;
导入android.widget.ImageButton;
导入android.widget.RatingBar;
导入android.widget.Toast;
导入静态???.MainActivity.REQUEST\u IMAGE\u CAPTURE;
公共类AddPlayder扩展AppCompative活动{
EditText playGroundName;
额定棒率;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent=getIntent();
setContentView(R.layout.activity\u add\u游乐场);
playGroundName=(EditText)findViewById(R.id.EditText\u操场\u名称);
比率=(比率条)findViewById(R.id.比率条);
Double message=intent.getDoubleExtra(MainActivity.EXTRA_message,0);
Toast.makeText(this,String.valueOf(message),Toast.LENGTH_SHORT.show();
}
公共void addPhoto(视图){
Intent takePictureIntent=新的意图(MediaStore.ACTION\u IMAGE\u CAPTURE);
if(takePictureContent.resolveActivity(getPackageManager())!=null){
startActivityForResult(获取图片内容、请求图像捕获);
}
}
}
我在*.xml文件中的代码如下:

<!-- begin snippet -->

    import android.app.Activity;
    import android.content.Intent;
    import android.provider.MediaStore;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ImageButton;
    import android.widget.RatingBar;
    import android.widget.Toast;

    import static ???.MainActivity.REQUEST_IMAGE_CAPTURE;

    public class AddPlayground extends AppCompatActivity {

      EditText playGroundName;
      RatingBar rate;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        setContentView(R.layout.activity_add_playground);
        playGroundName = (EditText) findViewById(R.id.editText_playground_name);
        rate = (RatingBar) findViewById(R.id.ratingBar);
        Double message = intent.getDoubleExtra(MainActivity.EXTRA_MESSAGE, 0);
        Toast.makeText(this, String.valueOf(message), Toast.LENGTH_SHORT).show();
      }

      public void addPhoto(View view) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
          }
      }

    }

<!-- end snippet -->
<!-- begin snippet -->
<?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="???.AddPlayground">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical" android:layout_width="368dp"
        android:layout_height="495dp"
        android:weightSum="1"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">
        <EditText
            android:id="@+id/editText_playground_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:hint="Name of playground" />
        <ImageButton
            android:id="@+id/imageButton_add_playground_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.52"
            android:onClick="addPhoto"
            app:srcCompat="@drawable/ic_menu_camera" />
        <RatingBar
            android:id="@+id/ratingBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="5"
            android:visibility="visible" />
        <EditText
            android:id="@+id/editText_playground_comment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textMultiLine"
            android:hint="Playground description" />
        <Button
            android:id="@+id/button_add_playground"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="addNewPlayground"
            android:text="Add" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>
<!-- end snippet -->


在下面???还有一些东西,但我删除了它,因为它对这个问题并不重要。

当你开始学习教程时,你应该阅读全文。。。您有完整的文档(您的方法
addPhoto
是其中
dispatchTakePictureIntent
方法的1:1副本)

简而言之,您还需要处理返回的位图(缩略图)


接收全尺寸的
位图
在上述链接文档的一节中进行了描述

安卓摄像头应用程序
在返回
意图
中对
照片
进行编码
,并将其作为
附加
中的一个小
位图
,在
键下的
数据

使用下面的代码检索
图像
,并将其显示在
图像按钮

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        imageButton.setImageBitmap(imageBitmap);
    }
}
以下是完整代码:

import android.app.Activity;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.Toast;


public class AddPlayground extends AppCompatActivity {

  EditText playGroundName;
  RatingBar rate;
  ImageButton imageButton;

  private static final int REQUEST_IMAGE_CAPTURE = 1;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_add_playground);
    playGroundName = (EditText) findViewById(R.id.editText_playground_name);
    rate = (RatingBar) findViewById(R.id.ratingBar);
    imageButton = (ImageButton) findViewById(R.id.imageButton_add_playground_image);

    Intent intent = getIntent();
    Double message = intent.getDoubleExtra(MainActivity.EXTRA_MESSAGE, 0);

    Toast.makeText(this, String.valueOf(message), Toast.LENGTH_SHORT).show();
  }

  public void addPhoto(View view) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
      }
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();

        if(extras != null) {
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            imageButton.setImageBitmap(imageBitmap);
        }
     }
  }

}
确保您在
AndroidManifest.xml
中添加了以下
uses feature
标记以使用
Camera
功能:

<manifest ... >
    <uses-feature android:name="android.hardware.camera"
                  android:required="true" />
    .......
    ..................
</manifest>

.......
..................

希望这会有所帮助~

谢谢你。这有点帮助,但它用非常小的图像(作为图标)取代了按钮,我想把它做成一张照片,适合按钮。