有人可以解决我的问题吗?
我想做一个活动,其中我有一个制作照片按钮(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 -->
在???下还有其他东西,但我删除了它,因为它对这个问题并不重要。
答案 0 :(得分:0)
当您开始使用教程时,您应该阅读全文... HERE您有完整的文档(您的方法addPhoto
是{1}}方法的1:1副本)< / p>
简而言之,您还需要处理返回的位图(缩略图)
dispatchTakePictureIntent
上面链接的文档的Save the Full-size Photo部分中描述了接收完整尺寸的@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);
}
}
答案 1 :(得分:0)
Android相机应用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>
希望这会有所帮助〜