Android 照相机赢得';t使用intent拍摄图像后关闭
我想创建一个简单的应用程序,打开相机,拍摄一张照片,在用户接受这张照片后,关闭相机并在图像视图中显示它。 请注意,现在我不想保存此图像,只想显示它。 我使用了android文档中的代码来实现这一点,但拍照后相机没有关闭,因此我无法在图像视图中看到照片。 代码取自android文档,因此它不能工作真的令人沮丧 谁知道为什么 提前谢谢 我的Java代码如下:Android 照相机赢得';t使用intent拍摄图像后关闭,android,camera,Android,Camera,我想创建一个简单的应用程序,打开相机,拍摄一张照片,在用户接受这张照片后,关闭相机并在图像视图中显示它。 请注意,现在我不想保存此图像,只想显示它。 我使用了android文档中的代码来实现这一点,但拍照后相机没有关闭,因此我无法在图像视图中看到照片。 代码取自android文档,因此它不能工作真的令人沮丧 谁知道为什么 提前谢谢 我的Java代码如下: @Override protected void onStart(){ super.onStart(); //here is
@Override
protected void onStart(){
super.onStart();
//here is the camera request
dispatchTakePictureIntent();
}
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
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();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ImageView mImageView = (ImageView) findViewById(R.id.fullscreen_content);
mImageView.setImageBitmap(imageBitmap);
}
}
我的布局是这样的(大部分是通过全屏活动准备的,所以唯一重要的是ImageView):
我还在清单中指定了uses功能,如下所示:
<uses-feature android:name="android.hardware.camera" android:required="false"/>
使用此代码通过android运行时权限实现此目的: 运行时许可证我们需要API 23或大于23 这是MainActivity.Java代码:这里我使用了运行时权限,如果您运行应用程序API 23或grater,这将显示一个
对话框
并询问权限。我在这里使用了startActivityForResult
,它将在捕获后返回图像
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.support.v4.app.ActivityCompat;
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.Toast;
public class MainActivity extends AppCompatActivity {
Button button ;
ImageView imageView ;
Intent intent ;
public static final int RequestPermissionCode = 1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
imageView = (ImageView)findViewById(R.id.imageView);
EnableRuntimePermission();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 7);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 7 && resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
}
public void EnableRuntimePermission(){
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.CAMERA))
{
Toast.makeText(MainActivity.this,"CAMERA permission allows us to Access CAMERA app", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(MainActivity.this,new String[]{
Manifest.permission.CAMERA}, RequestPermissionCode);
}
}
@Override
public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {
switch (RC) {
case RequestPermissionCode:
if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,"Permission Granted, Now your application can access CAMERA.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this,"Permission Canceled, Now your application cannot access CAMERA.", Toast.LENGTH_LONG).show();
}
break;
}
}
}
main_activity.xml代码:这里我使用了ImageView和按钮,单击按钮后它将捕获图像并显示到ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/activity_main"
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"
tools:context="com.android_examples.MainActivity"
android:orientation="vertical"
android:background="#FFF9C4">
<ImageView
android:layout_width="fill_parent"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:id="@+id/imageView" />
<Button
android:text="Click here to capture image using camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button" />
</LinearLayout>
Menifest代码:添加
以获得摄像头的许可
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android_examples.captureimagecamera_android_examplescom">
<uses-permission android:name="android.permission.CAMERA" />
<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>
代码取自android文档,因此它不能工作真的令人沮丧
您似乎认为问题在于文档。事实并非如此。这是与您选择的相机应用程序
ACTION\u IMAGE\u CAPTURE
将启动一些摄像头应用程序。大约10000个安卓设备型号上预装了几十个(如果不是几百个)不同的摄像头应用程序。用户可以从Play Store或其他地方安装几十个(如果不是几百个)额外的摄像头应用程序。您的代码将启动其中任何一个
有些摄像头应用程序有漏洞
你能直接做的很少。接受某些摄像头应用程序存在错误的事实,或者不使用
操作\u图像\u捕获
请详细解释这如何影响摄像头应用程序没有将控制权返回到启动摄像头应用程序的应用程序。@commonware,是的,我要编辑&我会解释所有的事情。你还有什么建议?@GiliJacobi:直接使用摄像头API(android.hardware.camera
,android.hardware.camera2.*
)或通过包装库(Fotoapparat,CameraKit-android等)。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android_examples.captureimagecamera_android_examplescom">
<uses-permission android:name="android.permission.CAMERA" />
<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>