您好,登錄后才能下訂單哦!
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); }
看完上述內容,你們掌握Android開發中如何實現調用Camera的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。