Android-Simplified photo taking (2)

Source: Internet
Author: User

Browse photos

If simplifying the photo taking process is not the main goal of your application, you can get the image it returns from the camera application and use it to do something.

The camera app of Android encodes a photo into a small bitmap object and places it into the returned intent object and sends it to the onactivityresult () method. The following code receives the photo and displays it in an imageview object.

Privatevoidhandlesmallcameraphoto (intent
Intent ){
Bundle extras = intent. getextras ();
Mimagebitmap = (Bitmap) extras. Get ("data ");
Mimageview. setimagebitmap (mimagebitmap );
}

Note: obtaining thumbnails from "data" is very useful, but not too many. Processing panorama requires more work.

Save photo

If you want to save the photo to a file, the android camera application will save a full-size photo. You must provide a storage volume label, folder, and file name.

The following is an easy way to obtain the photo path, but it is only valid after android2.2 (API Level 8:

Storagedir
= Newfile (
Environment. getexternalstoragepublicdirectory (
Environment. directory_pictures
),
Getalbumname ()
);

For the early API level, you must provide the photo directory name by yourself:

Storagedir
= Newfile (
Environment. getexternalstoragedirectory ()
+ Pictures_dir
+ Getalbumname ()
);

Note: The pictures_dir in the path is only pictures/, which is the standard location for sharing photos on external and shared storage.

Set file name

As mentioned above, the location where image files are stored must be driven by a device environment. All you need to do is select a file naming scheme to prevent conflicts. You may also want to save the path to a member variable for future use. For example:

Privatefile createimagefile () throwsioexception {
// Create an image filename
String timestamp =
New simpledateformat ("yyyymmdd_hhmmss"). Format (new date ());
String imagefilename = pai_file_prefix + timestamp + "_";
File image = file. createtempfile (
Imagefilename,
Pai_file_suffix,
Getalbumdir ()
);
Mcurrentphotopath = image. getabsolutepath ();
Return image;
}

Append the file name to the intent object.

After you have determined the location of the saved image, you can pass the location to the camera application through the intent object.

File F
= Createimagefile ();
Takepictureintent. putextra (mediastore. extra_output, Uri. fromfile (f ));

Add a photo to a gallery

When creating an image through an intent object, you should know the storage location of the image, because you saved it first. For other users, it is the best way to access your photos by using the system's media providers.

The method in the following example demonstrates how to call the system's media scanner and put your photos in the database of the media provider, make it effective in Android gallery applications and other applications.

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 );
}

Decode the scaled Image

Due to memory limitations, it is rare to manage multiple full-size images. If your application only displays a few images and the memory is leaked, You can scale the image to a size that matches the target view, then, the scaled JPEG image is expanded to the memory array, which can significantly reduce the usage of the dynamic heap. The following example demonstrates the technology:

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 );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.