Android Studio:如何在安卓相机拍摄照片后用照片替换图像按钮
问题描述:
有人可以帮我解决我的问题吗?
我想做一个活动,其中我有一个照片按钮(imageButton_add_playground_image),并按下它后,我的应用程序打开相机为我拍照(只有一张照片)。
制作完照片之后,我想将它发送到这个活动,并将照片按钮更改为用户制作的照片(但是在其上留下照片按钮活动,以便照片可以替换为不同的照片,如果这张照片不是这样不够好)。
我在* java文件代码如下:Android Studio:如何在安卓相机拍摄照片后用照片替换图像按钮
<!-- 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 -->
下???还有别的东西,但我删除了它,因为它对这个问题并不重要。
答
当你开始使用教程,你应该阅读全文... HERE你有充分的文档(你的方法addPhoto
为1:在那里复制的dispatchTakePictureIntent
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相机应用A的Save the Full-size Photo部分中描述encodes
photo
在返回Intent
交付onActivityResult()
作为一个小Bitmap
在extras
,在关键"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);
}
}
}
}
确保您已经添加下面uses-feature
标签在AndroidManifest.xml
使用Camera
功能:
<manifest ... >
<uses-feature android:name="android.hardware.camera"
android:required="true" />
.......
..................
</manifest>
希望这将帮助〜
比你是FAT。它有一点帮助,但它用非常小的图像(作为图标)取代了按钮,我想将它作为适合按钮的照片。 – Ventru