Android編程調(diào)用Camera和相冊(cè)功能詳解
本文實(shí)例講述了Android編程調(diào)用Camera和相冊(cè)功能。分享給大家供大家參考,具體如下:
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="相冊(cè)" />
<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() { //獲取相冊(cè)
@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 調(diào)用相冊(cè)里的圖片并返回
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);
在原來(lái)的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:關(guān)于AndroidManifest.xml文件相關(guān)屬性功能可參考本站在線工具:
Android Manifest功能與權(quán)限描述大全:
http://tools.jb51.net/table/AndroidManifest
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android拍照與圖片處理技巧總結(jié)》、《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android相機(jī)Camera基礎(chǔ)知識(shí)
- Android camera實(shí)時(shí)預(yù)覽 實(shí)時(shí)處理,人臉識(shí)別示例
- Android自定義照相機(jī)Camera出現(xiàn)黑屏的解決方法
- Android利用Camera實(shí)現(xiàn)中軸3D卡牌翻轉(zhuǎn)效果
- Android編程中調(diào)用Camera時(shí)預(yù)覽畫面有旋轉(zhuǎn)問(wèn)題的解決方法
- Android Camera是否支持變焦的判斷方法總結(jié)
- Android Camera變焦編程步驟
- Android camera2 判斷相機(jī)功能是否可控的實(shí)例
相關(guān)文章
Android中的ViewPager視圖滑動(dòng)切換類的入門實(shí)例教程
Android中ViewPager通常與Fragments組件共同使用來(lái)實(shí)現(xiàn)視圖切換功能,本文就帶大家一起來(lái)學(xué)習(xí)Android中的ViewPager視圖滑動(dòng)切換類的入門實(shí)例教程:2016-06-06
Android自定義控件實(shí)現(xiàn)帶文字提示的SeekBar
這篇文章主要給大家介紹了關(guān)于Android自定義控件實(shí)現(xiàn)帶文字提示的SeekBar的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
Android實(shí)現(xiàn)動(dòng)態(tài)自動(dòng)匹配輸入內(nèi)容功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)動(dòng)態(tài)自動(dòng)匹配輸入內(nèi)容功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android 在程序運(yùn)行時(shí)申請(qǐng)權(quán)限的實(shí)例講解
下面小編就為大家分享一篇Android 在程序運(yùn)行時(shí)申請(qǐng)權(quán)限的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

