Android编程调用Camera和相册功能详解
本文实例讲述了Android编程调用Camera和相册功能。分享给大家供大家参考,具体如下:
xml:
<LinearLayoutxmlns: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:
packagecom.wj.cameratest;
importjava.io.File;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.graphics.drawable.Drawable;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.os.Environment;
importandroid.provider.MediaStore;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.ImageView;
publicclassCameraShowActivityextendsActivity{
privateImageViewmImageView;
privateButtonmButtonCamera;
privateButtonmButtonPhoto;
@Override
publicvoidonCreate(BundlesavedInstanceState){
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(newOnClickListener(){//打开Camera
@Override
publicvoidonClick(Viewv){
Intentintent=newIntent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(newFile(Environment
.getExternalStorageDirectory(),"camera.jpg")));
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,0);
startActivityForResult(intent,10);
}
});
mButtonPhoto.setOnClickListener(newOnClickListener(){//获取相册
@Override
publicvoidonClick(Viewv){
Intentintent=newIntent(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
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
if(requestCode==10&&resultCode==Activity.RESULT_OK){
this.mImageView.setImageDrawable(Drawable.createFromPath(newFile(
Environment.getExternalStorageDirectory(),"camera.jpg")
.getAbsolutePath()));
System.out.println("data-->"+data);
}elseif(requestCode==11&&resultCode==Activity.RESULT_OK){
System.out.println("data2-->"+data);
}
}
}
Manifest.xml
<manifestxmlns: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-permissionandroid:name="android.permission.CAMERA"/> <uses-permissionandroid:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-featureandroid:name="android.hardware.camera"/> <uses-featureandroid: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> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
android调用相册里的图片并返回
Intentintent=newIntent(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
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
System.out.println(resultCode);
BitmapcameraBitmap=(Bitmap)data.getExtras().get("data");
super.onActivityResult(requestCode,resultCode,data);
}
PS:关于AndroidManifest.xml文件相关属性功能可参考本站在线工具:
AndroidManifest功能与权限描述大全:
http://tools.jb51.net/table/AndroidManifest
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android拍照与图片处理技巧总结》、《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。