zoukankan      html  css  js  c++  java
  • Android调用相机拍摄照片并显示到 ImageView控件中

    在前面的一篇文章中曾介绍过简单的开启相机照相功能,详见 Android简单调用相机Camera功能,实现打开照相功能 ,这一次就会将前面拍摄的照片显示到ImageView中,形成一个完整的效果 看实例 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 因此。我们须要将硬件加速功能关闭。

  • 相关阅读:
    纯jq编写增删改,弹出框
    li颜色特效
    省市联动Demo
    button轮番点击,只点击一次,鼠标hover
    正则--密码强度验证
    正则--验证邮箱与手机号
    正则表达式--元字符
    C#电脑自动关机代码指令
    1.ASP.NET MVC使用EPPlus,导出数据到Excel中
    6.在MVC中使用泛型仓储模式和依赖注入实现增删查改
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7400161.html
Copyright © 2011-2022 走看看