System already has something, if we do not have new requirements, direct call is the most direct. Here's how to call a system camera to take pictures and save pictures and how to call a system photo album.
First look at the core method of calling the system camera:
The code is as follows |
Copy Code |
Intent camera = new Intent (mediastore.action_image_capture); Startactivityforresult (camera, camera);
|
The data returned by the camera is obtained through the following callback method and processed:
The code is as follows |
Copy Code |
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
if (Requestcode = = CAMERA && ResultCode = ACTIVITY.RESULT_OK && null!= data) {
String sdstate=environment.getexternalstoragestate ();
if (!sdstate.equals (environment.media_mounted)) {
GameLog.log (Tag, "SD card unmount");
Return
}
New DateFormat ();
String name= Dateformat.format ("Yyyymmdd_hhmmss", Calendar.getinstance (Locale.china)) + ". jpg";
Bundle Bundle = Data.getextras ();
Gets the data returned by the camera and converts it to a picture format
Bitmap Bitmap = (Bitmap) bundle.get ("Data");
FileOutputStream fout = null;
File File = new file ("/sdcard/pintu/");
File.mkdirs ();
String Filename=file.getpath () +name;
try {
Fout = new FileOutputStream (filename);
Bitmap.compress (Bitmap.CompressFormat.JPEG, fout);
catch (FileNotFoundException e) {
E.printstacktrace ();
}finally{
try {
Fout.flush ();
Fout.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
Show pictures
} } |
Here's how to call a system photo album and get a Photo:
The code is as follows |
Copy Code |
Intent picture = new Intent (Intent.action_pick,android.provider.mediastore.images.media.external_content_uri); Startactivityforresult (picture, picture);
|
The following callback methods are appropriate:
The code is as follows |
Copy Code |
@Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) { Super.onactivityresult ( Requestcode, ResultCode, data); if (Requestcode = CAMERA && resultcode = ACTIVITY.RESULT_OK && null!= data) { Uri selectedimage = Data.getdata (); string[] filepathcolumns={ MediaStore.Images.Media.DATA}; Cursor C = this.getcontentresolver (). Query (SelectedImage, filepathcolumns, null,null, NULL); C.movetofirst (); int columnindex = C.getcolumnindex (Filepathcolumns[0]); String picturepath= c.getstring (columnindex); c.close (); //Get pictures and display } |
This completes the system call, very simple, but some friends will run into the photo shoot, in the phone album found photos do not show AH.
solve the problem of Android photos saved in the System album does not show
MediaStore.Images.Media.insertImage (Getcontentresolver (), Mbitmap, "", ""); you can insert it into the System gallery by the code above, and there's a problem Is that we can not specify the name of the Insert picture, but the system gives us a current time of the number of milliseconds for the name, there is a problem for a long time, I still first put the source of the insertimage to post it
The code is as follows |
Copy Code |
/**
* Insert an image and create a thumbnail for it.
*
* @param cr The content resolver to use
* @param source The stream to use for the image
* @param title The name of the image
* @param Description The description of the image
* @return The URL to the newly created image, or <code>null</code> if the image failed to is stored
* For any reason.
*/
public static final String insertimage (contentresolver cr, Bitmap Source,
string title, string description) {
Contentvalues values = new Contentvalues ();
Values.put (Images.Media.TITLE, TITLE);
Values.put (Images.Media.DESCRIPTION, DESCRIPTION);
Values.put (Images.Media.MIME_TYPE, "image/jpeg"); Uri URL = null; String stringurl = null; /* value to be returned * * try { url = Cr.insert (External_content_uri, values); if (source!= null) {
OutputStream imageout = cr.openoutputstream (URL);
try {
Source.compress (Bitmap.CompressFormat.JPEG, M, imageout);
finally {
Imageout.close ();
} Long id = contenturis.parseid (URL);
Wait until Mini_kind thumbnail is generated.
Bitmap minithumb = Images.Thumbnails.getThumbnail (CR, ID,
Images.Thumbnails.MINI_KIND, NULL);
This is a for backward compatibility.
Bitmap microthumb = Storethumbnail (CR, Minithumb, ID, 50F, 50F,
Images.Thumbnails.MICRO_KIND);
} else {
LOG.E (TAG, "Failed to create thumbnail, removing original");
Cr.delete (URL, null, NULL);
url = null;
}
catch (Exception e) {
LOG.E (TAG, "Failed to insert Image", e);
if (URL!= null) {
Cr.delete (URL, null, NULL);
url = null;
}
} if (URL!= null) { StringUrl = Url.tostring (); } return stringurl; } |
The above method has a title, I just thought it is possible to set the name of the picture, set up a bit, the original is not, depressed, which master know the title of this field is why, tell the younger brother, appreciate!
Of course, Android also provides a way to insert a system album, you can specify the name of the save picture, I put the source code posted
The code is as follows |
Copy Code |
/**
* Insert an image and create a thumbnail for it.
*
* @param cr The content resolver to use
* @param imagepath The path to the image to insert
* @param name The name of the image
* @param Description The description of the image
* @return The URL to the newly created image
* @throws FileNotFoundException
*/
public static final String insertimage (Contentresolver cr, String ImagePath,
String name, string description) throws FileNotFoundException {
Check if file exists with a FileInputStream
FileInputStream stream = new FileInputStream (ImagePath);
try {
Bitmap BM = bitmapfactory.decodefile (ImagePath);
String ret = Insertimage (CR, BM, name, description);
Bm.recycle ();
return ret;
finally {
try {
Stream.Close ();
catch (IOException e) {
}
}
} |
Ah ah, paste the source code I just found that this method calls the first method, this name is the above method of title, Dizzy dead, this more depressed, anyway I set title no effect, ask the master for the younger brother answer, first, we continue to say
The code above is plugged into the system album and needs A clockwork broadcast.
The code is as follows |
Copy Code |
Sendbroadcast (New Intent (intent.action_media_mounted, Uri.parse ("file://" + Environment.getexternalstoragedirectory ())); |
The above broadcast is to scan the entire SD card broadcast, if you have a lot of SD card inside will scan for a long time, in the scan we are unable to access the SD card, so the user reflects very bad, with the micro-letter friends know that the micro-letter to save pictures to the system album did not scan the entire SD card, so we use the following method
The code is as follows |
Copy Code |
Intent Intent = new Intent (intent.action_media_scanner_scan_file); Uri uri = uri.fromfile (New File ("/sdcard/image.jpg")); Intent.setdata (URI); Mcontext.sendbroadcast (Intent); |
or use Mediascannerconnection.
code is as follows |
copy code |
Final mediascannerconnection msc = new Mediascannerconnection (Mcontext, New mediascannerconnectionclient () { public void onmediascannerconnected () { msc.scanfile ("/sdcard/ Image.jpg "," Image/jpeg "); } public void onscancompleted (String path, Uri URI) { log.v (TAG, "Scan completed"); msc.disconnect (); } |
You're going to ask me, how do I get the path to the picture we just inserted? Oh, this own method gets, Insertimage (contentresolver cr, Bitmap source,string title, String description), this method returns to us is inserts the picture the URI, We can get the absolute path to the picture based on this URI.
The code is as follows |
Copy Code |
Private String Getfilepathbycontentresolver (context context, Uri Uri) {
if (null = = URI) {
return null;
}
Cursor C = context.getcontentresolver (). Query (URI, NULL, NULL, NULL, NULL);
String filePath = null;
if (null = = c) {
throw New IllegalArgumentException (
"Query on" + URI + "returns null result.");
}
try {
if ((C.getcount ()!= 1) | |!c.movetofirst ()) {
} else {
FilePath = c.getstring (
C.getcolumnindexorthrow (Mediacolumns.data));
}
finally {
C.close ();
}
return filePath;
} |
The absolute path of the picture is obtained according to the method above