【问题标题】:Trying to get my app to take a picture试图让我的应用拍照
【发布时间】:2017-09-16 08:23:24
【问题描述】:
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

【问题讨论】:

  • 请链接文档。
  • 分享您的 xml 布局和崩溃日志
  • 如果你想用图像裁剪器告诉我,我已经为相机粘贴了下面的代码。@Justin

标签: android


【解决方案1】:

使用这个

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);
        }
    }

别忘了在清单文件中添加权限

&lt;uses-permission android:name="android.permission.CAMERA"/&gt;

注意:

从 Android 6.0(API 级别 23)开始,用户在应用运行时而不是在安装应用时授予应用权限

更多信息read from docs

【讨论】:

  • 您好,非常感谢。现在可以了。但是我可以知道为什么你必须把 view 作为一个论点吗?因为在文档中,它不需要参数。
  • 嗨。 @NileshRathod 我如何在dispatchTakePictureIntent 中使用View 参数以及我应该使用哪个视图(ImageView)?
【解决方案2】:

试试这个

在按钮点击时调用此函数 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();
}

【讨论】:

    【解决方案3】:

    意图 cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 2019-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多