編輯:關於Android編程
本文為大家分享了Android相機、圖冊基本demo,供大家參考,具體內容如下
package com.example.democamera; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.View; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; static final int gallery = 1, camera = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv_picture); } /** * 啟動圖片畫廊 * * @param view */ public void startGallery(View view) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, gallery); } /** * 啟動相機 * * @param view */ public void startCamera(View view) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (isSDExsit()) { /* 創建存放圖片文件夾 */ File dir = new File(Environment.getExternalStorageDirectory() + "/my"); if (!dir.exists()) dir.mkdirs(); /* 設置圖片參數 得到原尺寸的圖片 */ File file = new File(dir, "aaa.jpg"); intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, Configuration.ORIENTATION_UNDEFINED); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); } startActivityForResult(intent, camera); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case gallery: /* 做判斷,防止返回報錯 */ if (data != null) { /* * 三種方式處理URI */ Uri uri = data.getData(); iv.setImageURI(uri); Bitmap bitmap = BitmapFactory .decodeFile(getPathByUri(uri, this)); iv.setImageBitmap(bitmap); Bitmap bitmap2 = getBitmapByUri(uri, this); iv.setImageBitmap(bitmap2); } break; case camera: if (data != null) {// 這是獲得縮略圖的方法 Bitmap b = (Bitmap) data.getExtras().get("data"); iv.setImageBitmap(b); } else {// 有sd卡得到圖片原圖 Bitmap bitmap = BitmapFactory.decodeFile("sdcard/my/aaa.jpg");//直接這樣做會有發生OOM的風險,Demo簡單這麼處理 iv.setImageBitmap(bitmap); } break; default: break; } } /** * Uri-->Bitmap * * @param uri * @param context * @return */ public static Bitmap getBitmapByUri(Uri uri, Context context) { Bitmap bitmap = null; try { bitmap = MediaStore.Images.Media.getBitmap( context.getContentResolver(), uri); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bitmap; } /** * Uri--->Path * * @param uri * @param context * @return */ public static String getPathByUri(Uri uri, Context context) { String path = null; String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null); if (cursor.moveToFirst()) { int index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); path = cursor.getString(index); } return path; } /** * 判斷SD卡是否存在 * * @return */ public static boolean isSDExsit() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { return true; } else { return false; } } }
下面分享具體Android 調用相機、打開相冊、裁剪圖片的實現代碼,內容如下
private ImageView iv_user_photo; private String fileName = ""; private File tempFile; private int crop = 300;// 裁剪大小 private static final int OPEN_CAMERA_CODE = 10; private static final int OPEN_GALLERY_CODE = 11; private static final int CROP_PHOTO_CODE = 12; private OnClickListener PopupWindowItemOnClick = new OnClickListener() { @Override public void onClick(View v) { menuWindow.dismiss(); switch (v.getId()) { // 拍照 case R.id.btn_camera: initFile(); openCamera(); break; // 相冊 case R.id.btn_gallery: initFile(); openGallery(); break; default: break; } } }; public void initFile() { if(fileName.equals("")) { if(FileUtil.existSDCard()) { String path = Environment.getExternalStorageDirectory() + File.separator + "JanuBookingOnline" + File.separator; FileUtil.mkdir(path); Logger.i("path:" + path); fileName = path + "user_head_photo.jpg"; tempFile = new File(fileName); } else { CommonUitl.toast(context, "請插入SD卡"); } } } /** * 調用相機 */ public void openCamera() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 打開相機 intent.putExtra("output", Uri.fromFile(tempFile)); startActivityForResult(intent, OPEN_CAMERA_CODE); } /** * 打開相冊 */ public void openGallery() { Intent intent = new Intent(Intent.ACTION_PICK);// 打開相冊 intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*"); intent.putExtra("output", Uri.fromFile(tempFile)); startActivityForResult(intent, OPEN_GALLERY_CODE); } /** * 裁剪圖片 * @param uri */ public void cropPhoto(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("output", Uri.fromFile(tempFile)); intent.putExtra("crop", true); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", crop); intent.putExtra("outputY", crop); startActivityForResult(intent, CROP_PHOTO_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == 1) return; switch (requestCode) { case OPEN_CAMERA_CODE: cropPhoto(Uri.fromFile(tempFile)); break; case OPEN_GALLERY_CODE: cropPhoto(data.getData()); break; case CROP_PHOTO_CODE: try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; Bitmap bitmap = BitmapFactory.decodeFile(fileName, options); if (bitmap != null) { iv_user_photo.setImageBitmap(bitmap); CommonUitl.sharedPreferences(context, AppConstants.USER_PHOTO, fileName); } } catch (Exception e) { e.printStackTrace(); } break; default: break; } super.onActivityResult(requestCode, resultCode, data); }
以上就是關於Android相機、圖冊的基本操作內容,希望對大家學習Android軟件編程有所幫助。
寫在前面現在,越來越多的App裡面使用了模糊效果,這種模糊效果稱之為高斯模糊。大家都知道,在Android平台上進行模糊渲染是一個相當耗CPU也相當耗時的操作,一旦處理不
1、傳感器簡介手機中內置的傳感器是一種微型的物理設備,它能夠探測、感受到外界的信號,並按一定規律轉換成我們所需要的信息。Android手機通常都會支持多種類型的傳感器,如
前言:近年來,智能電視的發展如火如荼,Googel 也在大力推進TV及穿帶設備的發展,在互聯網的風口,是豬也會飛,這句話並不是沒有道理的。傳統電視機廠商,基本都轉型致力於
java虛擬機基本結構:JVM是一個內存中的虛擬機,那它的存儲就是內存了,我們寫的所有類、常量、變量、方法都在內存中,因此明白java虛擬機的內存分配非常重要,本部分主要