Android调用相机拍摄照片并显示到 ImageView控件中
2017-08-20 15:19
483 查看
在前面的一篇文章中曾介绍过简单的开启相机照相功能,详见 Android简单调用相机Camera功能,实现打开照相功能 ,这一次就会将前面拍摄的照片显示到ImageView中,形成一个完整的效果
看实例
MainActivity.java
看一下 activity_main.xml
最好,再配置一下权限吧。看AndroidMainFest.xml
这个地方,还要注意的是 android:hardwareAccelerated="false" 这一句,假设我们不加这一句的情况下。会非常easy出现下面问题
Bitmap too large to be uploaded into a texture
因此。我们须要将硬件加速功能关闭。
看实例
MainActivity.java
package com.example.camera; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { /** Called when the activity is first created. */ String SD_CARD_TEMP_DIR; Bitmap myBitmap; private ImageView imageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator + "tmp.jpg";//设定照相后保存的文件名称,相似于缓存文件 imageView = (ImageView)findViewById(R.id.imageView1); Button btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent cameraIntent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(SD_CARD_TEMP_DIR))); startActivityForResult(cameraIntent, 0); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(requestCode == 0){ Log.d("requestCode", "Need 0"); if(resultCode == RESULT_OK){ Log.d("resultCode", "OK!!!" + SD_CARD_TEMP_DIR); myBitmap = BitmapFactory.decodeFile(SD_CARD_TEMP_DIR); imageView.setImageBitmap(myBitmap); }else{ Log.d("resultCode", "" + resultCode); } }else{ Log.d("requestCode", "Not Need"); } } }
看一下 activity_main.xml
<RelativeLayout 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" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="Button" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button1" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:src="@drawable/ic_launcher" /> </RelativeLayout>
最好,再配置一下权限吧。看AndroidMainFest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.camera" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > </uses-permission> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:hardwareAccelerated="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.camera.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
这个地方,还要注意的是 android:hardwareAccelerated="false" 这一句,假设我们不加这一句的情况下。会非常easy出现下面问题
Bitmap too large to be uploaded into a texture
因此。我们须要将硬件加速功能关闭。
相关文章推荐
- Android调用相机拍摄照片并显示到 ImageView控件中
- Android调用系统相册和相机选择图片并显示在imageview中
- 读取手机照片或相机拍摄的照片,将其显示在imageview上的onActivityResult方法
- Android实现点击ImageView拍照,并将相机拍摄的图片显示在ImageView上
- 关于真机测试的android选择图片在ImageView上显示,调用setImageBitmap无法显示和闪退、控件消失
- android拍照,截图,保存并显示在ImageView控件中
- Android开发第三方库glide和圆形控件circleimageview一起使用第一次进入无法显示问题
- Android 调用系统相机拍摄获取照片的两种方法实现实例
- Android ImageView控件 按下与松开显示不同的背景图片
- Android 调用系统相机拍摄获取照片的两种方法实现实例
- android--调用系统相机拍照,显示并保存照片
- Android 调用用相机拍照保存在本地,imageview根据路径读取图片
- Android 使用系统相机拍摄照片保存到本地并在系统相册中显示
- Android调用系统摄像头拍照并显示在ImageView上
- 【Android 开发】:UI控件之显示图片控件 ImageView 的使用方法
- Android:ImageView控件显示图片
- Android 调用相机拍摄照片或者从相册选取照片
- android--调用系统相机拍照,显示并保存照片 - zhengwen
- android调用摄像头拍照,并且用imageView显示出来
- Android 7.0调用相机拍照,返回后显示拍照照片