我正在尝试创建一个应用程序,我想在其中使用媒体API,然后打开相机并使用拍照并在
中显示照片MainActivity.java
package com.example.hsports.cameraapiuse;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.jar.Manifest;
public class MainActivity extends AppCompatActivity {
static final int REQUEST_IMAGE_CAPTURE = 1;
static final int MY_PERMISSIONS_REQUEST_CAMERA=2;
ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView=(ImageView)findViewById(R.id.mImageView);
}
public void dispatchTakePictureIntent() {
Intent takepicture=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takepicture.resolveActivity(getPackageManager())!=null) {
startActivityForResult(takepicture, 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();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
public void checkPermission(View view)
{
int permissionCheck=ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA);
}
public void grantPermission(View view)
{
if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)!=PackageManager.PERMISSION_GRANTED)
{
}
else
{
ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.CAMERA},MY_PERMISSIONS_REQUEST_CAMERA);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch(requestCode)
{
case MY_PERMISSIONS_REQUEST_CAMERA:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
dispatchTakePictureIntent();
} else {
}
return;
}
}
}
activity_main.xml(在这里我添加了一个textview和一个用于查看相机将拍摄的图像的imageView)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.hsports.cameraapiuse.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/permission"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mImageView"
/>
<Button
android:id="@+id/check_permission"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Check Permission"
android:onClick="checkPermission"
/>
<Button
android:id="@+id/request_permission"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Request Permission"
android:onClick="grantPermission"
/>
</LinearLayout>
Android_manifest.xml(在这里我已经指定了一些使用相机作为硬件的使用权限。)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hsports.cameraapiuse">
<uses-permission
android:name="android.permission.CAMERA"
android:required="true"
/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
执行此代码后,单击“请求权限”按钮后,我没有打开任何相机。 我希望相机打开并单击照片并显示在imageView中。
答案 0 :(得分:0)
根据您运行的Android版本(例如Marshmallow),您可能需要检查运行时持久性。
请查看以下链接,其中显示了相机的示例。
https://developer.android.com/training/permissions/requesting.html