`
sillycat
  • 浏览: 2551291 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Android UI(6)Building Apps with Multimedia - Managing Audio Playback and Capturi

阅读更多
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/

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics