安卓工作室:如何在安卓相机中拍照后用照片替换图像按钮





有人可以帮助我解决我遇到的问题吗?

我想做一个活动,其中我有一个制作照片按钮(imageButton_add_playground_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 -->

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

<!-- 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 -->

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

当你开始学习教程时,你应该阅读全文......在这里,您有完整的文档(您的方法addPhotodispatchTakePictureIntent方法的 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");
        yourImageView.setImageBitmap(imageBitmap);
    }
}

接收全尺寸Bitmap在上面链接的文档的保存全尺寸照片部分中进行了描述

Android 相机应用程序encodes返回中的photo Intent作为extras中的小Bitmap交付给onActivityResult(),在密钥"data"下。

使用以下代码检索image并将其显示在ImageButton中。

@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>

希望这会有所帮助~

最新更新