- 浏览: 2551040 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Android UI(6)Building Apps with Multimedia - Managing Audio Playback and Capturi
- 博客分类:
- Mobile Platform
Android UI(6)Building Apps with Multimedia - Managing Audio Playback and Capturing Photos
Managing Audio Playback
1. Controlling Your App's Volume and Playback
…snip…
2. Managing Audio Focus
…snip…
3. Dealing with Audio Output Hardware
Capturing Photos
Download the PhotoIntentActivity.zip.
It is working on the real device, not the emulators.
1. Taking Photos Simply
Request Camera Permission
<uses-feature android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
The first one is allowed us to access the camera.
The second one is allowed us to store the pictures into external storage.
Take a Photo with the Camera App
There are 2 ways to store and show the pictures. One is directly get the data from call back, and the other is get the bitmap from the local file. In the demo, there are small photo and big photo.
For small photo, send the intent to camera application, that application will take a photo and call back.
Button.OnClickListener mTakePicSmallOnClickListener = new Button.OnClickListener() { public void onClick(View v) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(takePictureIntent, SMALL_PHOTO_REQUEST); }
};
Binding the listener to the button.
Button picSBtn = (Button) findViewById(R.id.photo_small); setBtnListenerOrDisable(picSBtn, mTakePicSmallOnClickListener, MediaStore.ACTION_IMAGE_CAPTURE);
Before binding the listener to the button, check if there is camera application on the device.
protected void setBtnListenerOrDisable(Button btw, Button.OnClickListener onClickListener, String intentName) { if (isIntentAvailable(this, intentName)) { btn.setOnClickListener(onClickListener); } else { btn.setText("Not working " + btn.getText()); btn.setClickable(false); } }
protectedstaticboolean 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;
}
View the Photo
Here is how we handle and view the small photo.
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case BIG_PHOTO_REQUEST: {
…snip... break;
} // big photo
case SMALL_PHOTO_REQUEST: {
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
photoBitmap = (Bitmap) extras.get("data");
photoView.setImageBitmap(photoBitmap);
photoView.setVisibility(View.VISIBLE);
}
break;
} // small photo
}
Directly get the binary stream data from the call back bundle.
Dealing with the Big Photo
Button.OnClickListener mTakePicOnClickListener = new Button.OnClickListener() {
public void onClick(View v) { File f = null; Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); try { f = setUpPhotoFile(); currentPhotoPath = f.getAbsolutePath(); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); startActivityForResult(takePictureIntent, BIG_PHOTO_REQUEST); } catch (IOException e) { Log.d(TAG, e.getMessage()); f = null; currentPhotoPath = null; } }};
Before we send the intent to camera application, we need to add the directory and picture File as parameters.
Based on the version of android sdk on device, decide how to get the external storage.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { storageDirFactory = new FroyoAlbumDirFactoryImpl(); } else { storageDirFactory = new BasicAlbumDirFactoryImpl();
}
package com.sillycat.easyrestclientandroid.util.factory.impl;
import java.io.File;
import android.os.Environment;
import com.sillycat.easyrestclientandroid.util.factory.AlbumStorageDirFactory;
public class BasicAlbumDirFactoryImpl implements AlbumStorageDirFactory {
// Standard storage location for digital camera files
private static final String CAMERA_DIR = "/dcim/";
public File getAlbumStorageDir(String albumName) {
return new File(Environment.getExternalStorageDirectory() + CAMERA_DIR + albumName);
}
}
package com.sillycat.easyrestclientandroid.util.factory.impl;
import java.io.File;
import android.os.Environment;
import com.sillycat.easyrestclientandroid.util.factory.AlbumStorageDirFactory;
public class FroyoAlbumDirFactoryImpl implements AlbumStorageDirFactory {
public File getAlbumStorageDir(String albumName) {
return new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),albumName);
}
}
Find the album directory
private File getAlbumDir() {
File storageDir = null; if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { storageDir = storageDirFactory.getAlbumStorageDir(getAlbumName()); if (storageDir != null) { if (!storageDir.mkdirs()) { if (!storageDir.exists()) { Log.d(TAG, "failed to create directory"); return null; } } } } else { Log.d(TAG, "External storage is not mounted READ/WRITE."); } return storageDir;
}
Create and prepare the photo file before send the intent
private File createImageFile() throws IOException {
// Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_"; File albumF = getAlbumDir(); File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX,albumF); return imageF; }
private File setUpPhotoFile() throws IOException { File f = createImageFile(); currentPhotoPath = f.getAbsolutePath(); return f;
}
Getting Call Back from Camera Application for Big Photo
caseBIG_PHOTO_REQUEST: {
if (resultCode == RESULT_OK) { if (currentPhotoPath != null) { setPic(); //SCALA the picture galleryAddPic(); //send the picture announcement currentPhotoPath = null; } } break;
} // big photo
Adjust the size to save the memory
privatevoid setPic() {
/* There isn't enough memory to open up more than a couple camera photos */ /* So pre-scale the target bitmap into which the file is decoded */ /* Get the size of the ImageView */ int targetW = photoView.getWidth(); int targetH = photoView.getHeight(); /* Get the size of the image */ BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(currentPhotoPath, bmOptions); int photoW = bmOptions.outWidth; int photoH = bmOptions.outHeight; /* Figure out which way needs to be reduced less */ int scaleFactor = 1; if ((targetW > 0) && (targetH > 0)) { scaleFactor = Math.min(photoW / targetW, photoH / targetH);
} /* Set bitmap options to scale the image decode target */ bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = scaleFactor; bmOptions.inPurgeable = true; /* Decode the JPEG file into a Bitmap */ Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath, bmOptions); /* Associate the Bitmap to the ImageView */ photoView.setImageBitmap(bitmap); photoView.setVisibility(View.VISIBLE);
}
Send out the broadcast and store the file in our gallery
privatevoid galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); File f = new File(currentPhotoPath); Uri contentUri = Uri.fromFile(f); mediaScanIntent.setData(contentUri); this.sendBroadcast(mediaScanIntent);
}
Store and Restore the Data
protected void onSaveInstanceState(Bundle outState) {
outState.putParcelable(BITMAP_STORAGE_KEY, photoBitmap); super.onSaveInstanceState(outState); }
protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); photoBitmap = savedInstanceState.getParcelable(BITMAP_STORAGE_KEY); photoView.setImageBitmap(photoBitmap);
}
2. Recording Videos Simply
Next
3. Controlling the Camera
http://developer.android.com/training/camera/cameradirect.html
I do not plan to write the Camera application myself.
References:
http://developer.android.com/training/managing-audio/index.html
http://stackoverflow.com/questions/8505647/how-to-capture-the-photo-from-camera-on-android-emulator
customer android player
http://www.360doc.com/content/11/0309/11/474846_99476781.shtml
http://easymorse.googlecode.com/svn/trunk/
http://easymorse.googlecode.com/svn/trunk/android.customer.player/
Managing Audio Playback
1. Controlling Your App's Volume and Playback
…snip…
2. Managing Audio Focus
…snip…
3. Dealing with Audio Output Hardware
Capturing Photos
Download the PhotoIntentActivity.zip.
It is working on the real device, not the emulators.
1. Taking Photos Simply
Request Camera Permission
<uses-feature android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
The first one is allowed us to access the camera.
The second one is allowed us to store the pictures into external storage.
Take a Photo with the Camera App
There are 2 ways to store and show the pictures. One is directly get the data from call back, and the other is get the bitmap from the local file. In the demo, there are small photo and big photo.
For small photo, send the intent to camera application, that application will take a photo and call back.
Button.OnClickListener mTakePicSmallOnClickListener = new Button.OnClickListener() { public void onClick(View v) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(takePictureIntent, SMALL_PHOTO_REQUEST); }
};
Binding the listener to the button.
Button picSBtn = (Button) findViewById(R.id.photo_small); setBtnListenerOrDisable(picSBtn, mTakePicSmallOnClickListener, MediaStore.ACTION_IMAGE_CAPTURE);
Before binding the listener to the button, check if there is camera application on the device.
protected void setBtnListenerOrDisable(Button btw, Button.OnClickListener onClickListener, String intentName) { if (isIntentAvailable(this, intentName)) { btn.setOnClickListener(onClickListener); } else { btn.setText("Not working " + btn.getText()); btn.setClickable(false); } }
protectedstaticboolean 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;
}
View the Photo
Here is how we handle and view the small photo.
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case BIG_PHOTO_REQUEST: {
…snip... break;
} // big photo
case SMALL_PHOTO_REQUEST: {
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
photoBitmap = (Bitmap) extras.get("data");
photoView.setImageBitmap(photoBitmap);
photoView.setVisibility(View.VISIBLE);
}
break;
} // small photo
}
Directly get the binary stream data from the call back bundle.
Dealing with the Big Photo
Button.OnClickListener mTakePicOnClickListener = new Button.OnClickListener() {
public void onClick(View v) { File f = null; Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); try { f = setUpPhotoFile(); currentPhotoPath = f.getAbsolutePath(); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); startActivityForResult(takePictureIntent, BIG_PHOTO_REQUEST); } catch (IOException e) { Log.d(TAG, e.getMessage()); f = null; currentPhotoPath = null; } }};
Before we send the intent to camera application, we need to add the directory and picture File as parameters.
Based on the version of android sdk on device, decide how to get the external storage.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { storageDirFactory = new FroyoAlbumDirFactoryImpl(); } else { storageDirFactory = new BasicAlbumDirFactoryImpl();
}
package com.sillycat.easyrestclientandroid.util.factory.impl;
import java.io.File;
import android.os.Environment;
import com.sillycat.easyrestclientandroid.util.factory.AlbumStorageDirFactory;
public class BasicAlbumDirFactoryImpl implements AlbumStorageDirFactory {
// Standard storage location for digital camera files
private static final String CAMERA_DIR = "/dcim/";
public File getAlbumStorageDir(String albumName) {
return new File(Environment.getExternalStorageDirectory() + CAMERA_DIR + albumName);
}
}
package com.sillycat.easyrestclientandroid.util.factory.impl;
import java.io.File;
import android.os.Environment;
import com.sillycat.easyrestclientandroid.util.factory.AlbumStorageDirFactory;
public class FroyoAlbumDirFactoryImpl implements AlbumStorageDirFactory {
public File getAlbumStorageDir(String albumName) {
return new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),albumName);
}
}
Find the album directory
private File getAlbumDir() {
File storageDir = null; if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { storageDir = storageDirFactory.getAlbumStorageDir(getAlbumName()); if (storageDir != null) { if (!storageDir.mkdirs()) { if (!storageDir.exists()) { Log.d(TAG, "failed to create directory"); return null; } } } } else { Log.d(TAG, "External storage is not mounted READ/WRITE."); } return storageDir;
}
Create and prepare the photo file before send the intent
private File createImageFile() throws IOException {
// Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_"; File albumF = getAlbumDir(); File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX,albumF); return imageF; }
private File setUpPhotoFile() throws IOException { File f = createImageFile(); currentPhotoPath = f.getAbsolutePath(); return f;
}
Getting Call Back from Camera Application for Big Photo
caseBIG_PHOTO_REQUEST: {
if (resultCode == RESULT_OK) { if (currentPhotoPath != null) { setPic(); //SCALA the picture galleryAddPic(); //send the picture announcement currentPhotoPath = null; } } break;
} // big photo
Adjust the size to save the memory
privatevoid setPic() {
/* There isn't enough memory to open up more than a couple camera photos */ /* So pre-scale the target bitmap into which the file is decoded */ /* Get the size of the ImageView */ int targetW = photoView.getWidth(); int targetH = photoView.getHeight(); /* Get the size of the image */ BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(currentPhotoPath, bmOptions); int photoW = bmOptions.outWidth; int photoH = bmOptions.outHeight; /* Figure out which way needs to be reduced less */ int scaleFactor = 1; if ((targetW > 0) && (targetH > 0)) { scaleFactor = Math.min(photoW / targetW, photoH / targetH);
} /* Set bitmap options to scale the image decode target */ bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = scaleFactor; bmOptions.inPurgeable = true; /* Decode the JPEG file into a Bitmap */ Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath, bmOptions); /* Associate the Bitmap to the ImageView */ photoView.setImageBitmap(bitmap); photoView.setVisibility(View.VISIBLE);
}
Send out the broadcast and store the file in our gallery
privatevoid galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); File f = new File(currentPhotoPath); Uri contentUri = Uri.fromFile(f); mediaScanIntent.setData(contentUri); this.sendBroadcast(mediaScanIntent);
}
Store and Restore the Data
protected void onSaveInstanceState(Bundle outState) {
outState.putParcelable(BITMAP_STORAGE_KEY, photoBitmap); super.onSaveInstanceState(outState); }
protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); photoBitmap = savedInstanceState.getParcelable(BITMAP_STORAGE_KEY); photoView.setImageBitmap(photoBitmap);
}
2. Recording Videos Simply
Next
3. Controlling the Camera
http://developer.android.com/training/camera/cameradirect.html
I do not plan to write the Camera application myself.
References:
http://developer.android.com/training/managing-audio/index.html
http://stackoverflow.com/questions/8505647/how-to-capture-the-photo-from-camera-on-android-emulator
customer android player
http://www.360doc.com/content/11/0309/11/474846_99476781.shtml
http://easymorse.googlecode.com/svn/trunk/
http://easymorse.googlecode.com/svn/trunk/android.customer.player/
发表评论
-
ionic UI(4)ionic2 framework - basic and components and native
2016-03-24 02:33 1261ionic UI(4)ionic2 framework - b ... -
ionic UI(3)TypeScript - handbook
2016-03-22 23:21 634ionic UI(3)TypeScript - handboo ... -
ionic UI(2)ionic2 framework - TypeScript - tutorial
2016-03-22 06:52 1656ionic UI(2)ionic2 framework - T ... -
Parse and Heroku Service(3)Parse Server and Parse Dashboard
2016-03-22 06:30 967Parse and Heroku Service(3)Pars ... -
Parse and Heroku Service(2)Mail Templates and Push Notification
2016-03-22 02:45 583Parse and Heroku Service(2)Mail ... -
ionic UI(1)Introduction
2016-03-19 03:18 723ionic UI(1)Introduction 1 Inst ... -
Parse and Heroku Service(1)Heroku Installation and Play
2016-03-19 00:13 824Parse and Heroic Service(1)Hero ... -
Hybrid(5)Customize Meteor Directly Google Login
2015-09-01 02:33 910Hybrid(5)Customize Meteor Direc ... -
Hybrid(4)Favorite Places - Google Login
2015-09-01 02:02 1341Hybrid(4)Favorite Places - Goog ... -
Hybrid(3)More Meteor Example - Social
2015-08-11 05:04 756Hybrid(3)More Meteor Example - ... -
Hybrid(2)meteor Running Android and iOS
2015-07-28 23:59 1049Hybrid(2)meteor Running Android ... -
Create the Google Play Account
2015-07-18 06:42 1101Create the Google Play Account ... -
Secure REST API and Mobile(1)Document Read and Understand OAUTH2
2015-07-14 00:36 764Secure REST API and Mobile(1)Do ... -
Screen Size and Web Design
2015-07-11 01:11 725Screen Size and Web Design iPh ... -
Hybrid(1)ionic Cordova meteor
2015-06-25 05:49 471Hybrid(1)ionic Cordova meteor ... -
Android Fire Project(1)Recall Env and Knowledge
2015-02-11 12:28 686Android Fire Project(1)Recall ... -
Android Content Framework(1)Concept
2014-06-14 13:54 1080Android Content Framework(1)Con ... -
Feel Android Studio(1)Install and Update Android Studio
2014-04-11 03:12 2029Feel Android Studio(1)Install a ... -
IOS7 App Development Essentials(2)iBeacon
2014-03-05 05:55 890IOS7 App Development Essentials ... -
IOS7 App Development Essentials(1) Persistent Store
2014-03-05 05:54 1327IOS7 App Development Essentials ...
相关推荐
Multimedia handling with a fast audio and video encoder Multimedia handling with a fast audio and video encode Multimedia handling with a fast audio and video encode Multimedia handling with a fast ...
官方离线安装包,亲测可用
Learning Swift: Building Apps for macOS, iOS, and Beyond by Paris Buttfield-Addison English | 30 Mar. 2017 | ASIN: B06XYGFYQT | 516 Pages | AZW3 | 8.92 MB Get valuable hands-on experience with Swift ...
本示例"wav-file-playback.rar_playback"提供了一个简单的WAV音频播放的实现,它利用了Windows API中的WinMM(Windows Multimedia)库。下面我们将深入探讨这个话题。 首先,WAV是一种无损音频格式,它保留了原始...
Learning Swift: Building Apps for macOS, iOS, and Beyond, 2 edition by Paris Buttfield-Addison and Jon Manning English | 2017 | ISBN: 1491967064 | 516 pages | PDF + EPUB | 24,7 + 22,5 MB Get valuable...
Learning Swift: Building Apps for OS X and iOS by Paris Buttfield-Addison, Jon Manning, Tim Nugent 2016 | ISBN: 1491940743 | English | 536 pages Get hands-on experience with Apple’s Swift ...
标题:“Signal-Processing-for-Image-Enhancement-and-Multimedia-Processing”表明了书籍的主题是信号处理技术在图像增强和多媒体处理领域的应用。信号处理是一个广泛而深入的领域,它包括数据的分析、处理、解释...
qtmultimedia-everywhere-src-6.6.0.zip
The approach is described in the paper Geotagging Text Content With Language Models and Feature Mining. Main Method The approach is a refined language model, including feature selection and weighting...
Python Multimedia - Beginner&.pdf
qtmultimedia-everywhere-src-6.6.0.tar.xz
"qt5-qtmultimedia-devel-5.6.0-1.nd7.3.x86_64.rpm"是一个针对NVIDIA Development Platform 7.3 (ND7.3)的RPM软件包,用于x86_64架构的系统。这个包是专门为开发人员设计的,包含了Qt5多媒体模块的开发头文件、库和...
Anybody can start building multimedia apps for the Android platform, and this book will show you how! Now updated to include both Android 4.4 and the new Android L, Android Apps for Absolute Beginners...
根据给定文件的信息,我们可以提炼出以下几个主要的知识点: ### 一、文件基本信息 #### 标题:《生物信息学、多媒体与电子电路进展——2020》 该书汇集了生物信息学、多媒体技术和电子电路领域的最新研究成果。...
Introduction To Computing And Programming With Java - A Multimedia Approach (2006),英文书籍,仅供个人参考,谢绝商业用途。
CSR-BlueCore5-Multimedia-External-CS-101568-DSP4(蓝牙多媒体芯片-csr-bc05-mm)原理图