浏览照片

如果简化拍照过程不是你的应用程序所追求的主要目标,那么你可以从照相机应用程序中获取其返回的图片,并使用它来做一些事情。

Android的Camera应用程序把照片编码成一个小的Bitmap对象,并把它放到返回的Intent对象中发送给onActivityResult()方法。下列代码接收这张照片,并在一个ImageView对象中显示。

privatevoidhandleSmallCameraPhoto(Intent intent){
    Bundle extras = intent.getExtras();
     mImageBitmap = (Bitmap) extras.get("data");
     mImageView.setImageBitmap(mImageBitmap);
 }

注意:从“data”中获取缩略图是很好用的,但不能太多。处理全景图需要更多的工作。

保存照片

如果你要把照片保存到一个文件中,那么Android的Camera应用程序会保存一个全尺寸的照片。你必须提供一个包含存储器卷标、文件夹和文件名。

以下是获取照片路径的比较容易的方法,但是只在Android2.2(API Level 8)以后才有效:

storageDir =newFile(
    Environment.getExternalStoragePublicDirectory(
         Environment.DIRECTORY_PICTURES
     ), 
     getAlbumName()
 );    对于早期的API Level,你必须自己提供照片目录名称:
storageDir =newFile(
    Environment.getExternalStorageDirectory()
         + PICTURES_DIR
         + getAlbumName()
 );

注意:路径中的PICTURES_DIR只是Pictures/,它是外部和共享存储器上共享照片的标准位置。

设置文件名称

如前所述,存放图片文件的位置应该是有设备环境来驱动的。你所需要做的是选择一个防止冲突的文件命名方案。你还可能希望把这个路径保存到一个成员变量中,以便以后使用,例如:

privateFile createImageFile()throwsIOException{
    // Create an image filename
     String 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对象中

确定了保存图片的位置后,你就可以通过Intent对象把这个位置传递给Camera应用程序。

File f =createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));

把照片添加到一个Gallery(画册)中

你通过Intent对象创建图片时,你应该知道图片的存放位置,因为你是最先保存它的。对于其他用户,让图片可以从系统的Media提供器中来方法,是访问你的照片的最好的方法。

下例的方法演示了如何调用系统的媒体扫描器,把你的照片放到的Media提供器的数据库中,让它在Android的Gallery应用程序和其他的应用程序中有效。

privatevoidgalleryAddPic(){
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
     File f = new File(mCurrentPhotoPath);
     UricontentUri = Uri.fromFile(f);
     mediaScanIntent.setData(contentUri);
     this.sendBroadcast(mediaScanIntent);
 }

解码被缩放的图片

由于内存的限制,管理多个全尺寸的图片是很难得。如果发现你的应用程序只显示几张图片后就内存泄露了,你可以把图片缩放到与目标View相匹配的尺寸,然后再把缩放后的JPEG图片展开到内存数组中,这样可以显著地降低动态堆的使用。下例方法演示了这种技术:

privatevoid setPic(){
    // Get the dimensions ofthe View
     inttargetW = mImageView.getWidth();
     inttargetH = mImageView.getHeight();
   
     // Get the dimensions ofthe bitmap
     BitmapFactory.Options bmOptions = new BitmapFactory.Options();
     bmOptions.inJustDecodeBounds = true;
     BitmapFactory.decodeFile(mCurrentPhotoPath,bmOptions);
     intphotoW = bmOptions.outWidth;
     intphotoH = bmOptions.outHeight;
   
     // Determine how much toscale down the image
     intscaleFactor = Math.min(photoW/targetW, photoH/targetH);
   
     // Decode the image fileinto a Bitmap sized to fill the View
     bmOptions.inJustDecodeBounds = false;
     bmOptions.inSampleSize =scaleFactor;
     bmOptions.inPurgeable = true;
   
     Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
     mImageView.setImageBitmap(bitmap);
 }