广告
返回顶部
首页 > 资讯 > 移动开发 >Android编程调用Camera和相册功能详解
  • 666
分享到

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

Android 2022-06-06 04:06:50 666人浏览 八月长安
摘要

本文实例讲述了Android编程调用Camera和相册功能。分享给大家供大家参考,具体如下: xml: <LinearLayout xmlns:android="Htt

本文实例讲述了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程序设计有所帮助。

您可能感兴趣的文章:Android相机Camera基础知识Android camera实时预览 实时处理,人脸识别示例Android自定义照相机Camera出现黑屏的解决方法Android利用Camera实现中轴3D卡牌翻转效果Android编程中调用Camera时预览画面有旋转问题的解决方法Android Camera是否支持变焦的判断方法总结Android Camera变焦编程步骤Android camera2 判断相机功能是否可控的实例


--结束END--

本文标题: Android编程调用Camera和相册功能详解

本文链接: https://www.lsjlt.com/news/22105.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作