欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程调用Camera和相册功能详解

程序员文章站 2023-12-17 15:35:52
本文实例讲述了android编程调用camera和相册功能。分享给大家供大家参考,具体如下: xml:

本文实例讲述了android编程调用camera和相册功能。分享给大家供大家参考,具体如下:

xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <button
    android:id="@+id/button_camerabutton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="拍照" />
  <button
    android:id="@+id/button_photobutton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="相册" />
  <imageview
    android:id="@+id/imageview_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:src="@drawable/ic_launcher" />
</linearlayout>

activity:

package com.wj.cameratest;
import java.io.file;
import android.app.activity;
import android.content.intent;
import android.graphics.drawable.drawable;
import android.net.uri;
import android.os.bundle;
import android.os.environment;
import android.provider.mediastore;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.imageview;
public class camerashowactivity extends activity {
  private imageview mimageview;
  private button mbuttoncamera;
  private button mbuttonphoto;
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_camera_show);
    mimageview = (imageview) this.findviewbyid(r.id.imageview_preview);
    mbuttoncamera = (button) this.findviewbyid(r.id.button_camerabutton);
    mbuttonphoto = (button) this.findviewbyid(r.id.button_photobutton);
    mbuttoncamera.setonclicklistener(new onclicklistener() { //打开camera
      @override
      public void onclick(view v) {
        intent intent = new intent("android.media.action.image_capture");
        intent.putextra(mediastore.extra_output,
            uri.fromfile(new file(environment
                .getexternalstoragedirectory(), "camera.jpg")));
        intent.putextra(mediastore.extra_video_quality, 0);
        startactivityforresult(intent, 10);
      }
    });
    mbuttonphoto.setonclicklistener(new onclicklistener() { //获取相册
      @override
      public void onclick(view v) {
        intent intent = new intent(intent.action_get_content);
        intent.addcategory(intent.category_openable);
        intent.settype("image/*");
        intent.putextra("crop", "true");
        intent.putextra("aspectx",1);
        intent.putextra("aspecty",1);
        intent.putextra("outputx", 80);
        intent.putextra("outputy", 80);
        intent.putextra("return-data",true);
        startactivityforresult(intent, 11);
      }
    });
  }
  @override
  protected void onactivityresult(int requestcode, int resultcode, intent data) {
    if (requestcode == 10 && resultcode == activity.result_ok) {
      this.mimageview.setimagedrawable(drawable.createfrompath(new file(
          environment.getexternalstoragedirectory(), "camera.jpg")
          .getabsolutepath()));
      system.out.println("data-->"+data);
    }else if (requestcode == 11 && resultcode ==activity.result_ok) {
      system.out.println("data2-->"+data);
    }
  }
}

manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.wj.cameratest"
  android:versioncode="1"
  android:versionname="1.0" >
  <uses-sdk
    android:minsdkversion="8"
    android:targetsdkversion="15" />
  <uses-permission android:name="android.permission.camera" />
  <uses-permission android:name="android.permission.mount_unmount_filesystems" />
  <uses-permission android:name="android.permission.write_external_storage" />
  <uses-feature android:name="android.hardware.camera" />
  <uses-feature android:name="android.hardware.camera.autofocus" />
  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/apptheme" >
    <activity
      android:name=".camerashowactivity"
      android:label="@string/title_activity_camera_show" >
      <intent-filter>
        <action android:name="android.intent.action.main" />
        <category android:name="android.intent.category.launcher" />
      </intent-filter>
    </activity>
  </application>
</manifest>

android 调用相册里的图片并返回

intent intent=new intent(intent.action_get_content);
intent.addcategory(intent.category_openable);
intent.settype("image/*");
intent.putextra("crop", "true");
intent.putextra("aspectx", 1);
intent.putextra("aspecty", 1);
intent.putextra("outputx", 80);
intent.putextra("outputy", 80);
intent.putextra("return-data", true);
startactivityforresult(intent, 0);

在原来的activity中如下获取选到的图片:

@override
 protected void onactivityresult(int requestcode, int resultcode, intent data) {
 system.out.println(resultcode);
 bitmap camerabitmap = (bitmap) data.getextras().get("data");
 super.onactivityresult(requestcode, resultcode, data);
 }

ps:关于androidmanifest.xml文件相关属性功能可参考本站在线工具:

android manifest功能与权限描述大全:
http://tools.jb51.net/table/androidmanifest

更多关于android相关内容感兴趣的读者可查看本站专题:《android拍照与图片处理技巧总结》、《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

上一篇:

下一篇: