【问题标题】:Android Studio: How to replace image button with photo after taking a photo in android cameraAndroid Studio:在Android相机中拍照后如何用照片替换图像按钮
【发布时间】:2017-05-25 18:51:14
【问题描述】:



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

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

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

【问题讨论】:

    标签: android android-camera


    【解决方案1】:

    当你开始学习教程时,你应该阅读全文...HERE 你有完整的文档(你的方法 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 在上述链接文档的Save the Full-size Photo 部分中进行了描述

    【讨论】:

      【解决方案2】:

      Android Camera 应用程序encodes 中的photo 返回Intent,作为extras 中的小Bitmap 传递给Bitmap,在键"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>
      

      希望对你有所帮助~

      【讨论】:

      • 谢谢 FAT。它有点帮助,但它用非常小的图像(作为图标)替换了按钮,我想把它做成一张适合按钮的照片。
      猜你喜欢
      • 1970-01-01
      • 2015-11-27
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多