Training—Capturing Photos
生活随笔
收集整理的這篇文章主要介紹了
Training—Capturing Photos
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
閱讀:https://developer.android.com/training/camera/index.html
先學學怎么從相機獲取相片。
首先要有權限:
<manifest ... ><uses-feature android:name="android.hardware.camera" />... </manifest ... > private void dispatchTakePictureIntent(int actionCode) {Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(takePictureIntent, actionCode); } //查詢是否有程序能夠照相 public static boolean isIntentAvailable(Context context, String action) {final PackageManager packageManager = context.getPackageManager();final Intent intent = new Intent(action);List<ResolveInfo> list =packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);return list.size() > 0; } //獲取Bitmap并顯示 private void handleSmallCameraPhoto(Intent intent) {Bundle extras = intent.getExtras();mImageBitmap = (Bitmap) extras.get("data");mImageView.setImageBitmap(mImageBitmap); } //保存圖像 storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), getAlbumName() ); //或者用下面的目錄 storageDir = new File (Environment.getExternalStorageDirectory()+ PICTURES_DIR+ getAlbumName() );private File createImageFile() throws IOException {// Create an image file nameString timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";File image = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, getAlbumDir());mCurrentPhotoPath = image.getAbsolutePath();return image; }如果要向INTENT存放圖片數據,用:
File f = createImageFile(); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));The following example method demonstrates how to invoke the system's media scanner to add your photo to the Media Provider's database, making it available in the Android Gallery application and to other apps.
private void galleryAddPic() {Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);File f = new File(mCurrentPhotoPath);Uri contentUri = Uri.fromFile(f);mediaScanIntent.setData(contentUri);this.sendBroadcast(mediaScanIntent); }下面展示了如何實現縮略圖功能(可減少內存使用):
private void setPic() {// Get the dimensions of the Viewint targetW = mImageView.getWidth();int targetH = mImageView.getHeight();// Get the dimensions of the bitmapBitmapFactory.Options bmOptions = new BitmapFactory.Options();bmOptions.inJustDecodeBounds = true;BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);int photoW = bmOptions.outWidth;int photoH = bmOptions.outHeight;// Determine how much to scale down the imageint scaleFactor = Math.min(photoW/targetW, photoH/targetH);// Decode the image file into a Bitmap sized to fill the ViewbmOptions.inJustDecodeBounds = false;bmOptions.inSampleSize = scaleFactor;bmOptions.inPurgeable = true;Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);mImageView.setImageBitmap(bitmap); }?官方接下來的內容還展示了如何獲取錄下的視頻以及相機的控制,請自行查看。
?
官方還提供了一些基礎的組件讓你顯示圖片還有HTML:
https://developer.android.com/training/printing/index.html
?
?
?
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/yutoulck/p/3407412.html
總結
以上是生活随笔為你收集整理的Training—Capturing Photos的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sql 定时同步两个数据库
- 下一篇: HUD 1043 Eight 八数码问题