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

Android编程调用Camera和相册功能详解,第1张

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

本文实例讲述了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.@R_301_6852@;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.from@R_301_6852@(new @R_301_6852@(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 @R_301_6852@(          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_@R_301_6852@SYstemS" />  <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,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程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Android编程调用Camera和相册功能详解全部内容,希望文章能够帮你解决Android编程调用Camera和相册功能详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1146803.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存