package com.example.android.health3;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView result;
static final int REQUEST_IMAGE_CAPTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
result = (ImageView) findViewById(R.id.imageView);
}
public void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
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");
result.setImageBitmap(imageBitmap);
}
}
}
它应该是一个非常简单的概念。我完全按照Android开发人员的文档说的做了,但是这段代码没有运行......当我按下按钮拍照时,应用停止。
如果你知道原因,请告诉我。 :(
<?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="com.example.android.health3.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="dispatchTakePictureIntent"
android:text="Button"
/>
<ImageView
android:id="@+id/imageView"
android:layout_width="208dp"
android:layout_height="231dp"
/>
</android.support.constraint.ConstraintLayout>
文档:https://developer.android.com/training/camera/photobasics.html
答案 0 :(得分:1)
试试这个
在按钮Onclick上调用此函数takePicture
private void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String dateCreated = sdf.format(new Date(System.currentTimeMillis()));
f = new File(getExternalCacheDir(), "IMGPLk-" + dateCreated.trim() + ".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
//intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
//finish();
}
答案 1 :(得分:0)
使用此
public void dispatchTakePictureIntent(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
这是
的内容public void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
不要忘记在清单文件中添加权限
<uses-permission android:name="android.permission.CAMERA"/>
注意:
从Android 6.0(API级别23)开始,用户在应用运行时向应用授予权限,而不是在他们安装应用时授予
了解更多信息 read from docs
答案 2 :(得分:0)
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent,CAMERA_REQUEST);