Android gets multiple recent photos from the album (get the photos stored by the camera)
When you are working on a company project, you need to customize the grid view of the displayed photos to display the first 20 photos that you have taken with a recent camera.
I checked the API provided by the system and did not find the path constant pointing directly to storing photos after taking photos with a camera. If anyone knows, please let me know. Thank you!
The following is an example of my stupi
When you call a camera, you sometimes need to return an image. Sometimes, you do not need to return an image.
Case 1,Call the camera in the app,Save the image to the specified directory., Returned to the app:
PublicVoidStartintentforresult (){
Intent intent =NewIntent (mediastore. action_image_capture );
Intent. putextra (mediastore. extra_output, Uri. fromfile (mfile ));
Startactivityfo
In a recent project, you need to find the URI of the image through the absolute path of the image, delete the image, and summarize the method for obtaining the URI.
Android 4.1.3 systems are available.
1. Retrieve all image paths, traverse and compare to find the desired path, and retrieve the URI, which is less efficient.
The MediaStore. MediaColumns. DATA Field stores the absolute path of the image,
At last, mImageUri obtains the image URI.
1 Uri m
Call the camera program and add it to intent.Android. provider. mediastore. extra_outputParameter to set the image storage location. As follows:
Java code
String imagefilepath = environment. getexternalstoragedirectory (). getabsolutepath ()
+ "/Myfavoritepicture.jpg ";
File imagefile = new file (imagefilepath );
Uri imagefileuri = URI. fromfile (imagefile );
// URI imagefileuri = URI. parse ("file: // sdcard/myfavoritepicture.jpg ");
Inte
Android practice simple tutorial-28th gun (Uri to String type instance), androiduri
After reading the previous article, we can easily obtain the uri of the selected image. So how can we convert the obtained uri to a String address?
Next we will study it through examples. The layout file is the same as the previous article (article 27), so we will not list it any more. Let's look at MainActivity. java:
Package com. example. userphoto; import java. io. file; import android. app. activity; import
Main Program:
Public class androcamera extends activity {
Private Static final int image_capture = 0;
Private button startbtn;
Private URI imageuri;
Private imageview;
/** Called when the activity is first created.* Sets the content and gets the references* The basic widgets on the screen like* {@ Code button} or {@ link imageview}*/@ OverridePublic void oncreate (bundle savedinstancestate ){Super. oncreate (savedinstancestate );Setcontentview (R. layout. Main );Imageview = (imageview) fin
String [] projection = {mediastore. Images. Media. size,
Mediastore. Images. Media. display_name };
Uri uri = mediastore. Images. thumbnails. getcontenturi ("external ");
Cursor c = thumbnails. queryminithumbnails (getcontentresolver (), Uri,
The code in the second line indicates the URI of the path for obtaining the sdcard.
Uri uri =
update the UI interface.
After talking about this, you may think that the cloud is in the fog. Let's look at the actual example below.2. Implementation of the Image Library Function
This program first scans all the folders containing images on the mobile phone, and finally selects the most picture folder, and displays the images in the folder using the GridView.1. layout File
The layout file is quite simple, just like a GridView
2. MainActivity
Package com. example. zhy_han
. Speaking of this, I will mention the method of analyzing the source code. If you have a little knowledge of the Android source code, you should know that the Android source code is very huge, therefore, the starting point of the analysis program can be roughly divided into two types: the first is to analyze the source code according to the operating procedures-applicable to programs with clear interface jump; the second is to analyze the program running logic based on the printed Log informati
Testing Machine: Xiaomi 2.3.5There are actually a few lines of code. Here we will simply record the learning process.When the Android system starts, It scans the system and SD card for media files, stores them in the sqlite database, and provides external services in the form of contentProvider.Path:/data/com. android. providers. media/databases/XXX...We can see two db files, one in the system and the other in the SD card.Use SQLite Expert to open internal. db, as shown in the following section:
1. android manages multimedia files (audio, video, and image. In the/data/com. android. providers. in media, find the database file and open external. for more information about the db file, see the ing between the file path (_ data) and the Uri ID (_ id) in the media table. Ii. URI and file conversion 1. obtain the file path [java] string myImageUrl = "content: // media/external/images/media/***" from URI; Uri uri Uri = URI. parse (myImageUrl); String [] proj = {
. createChooser (it, "Choose Email Client "));
Intent it = new Intent (Intent. ACTION_SEND );
String [] tos = {"me@abc.com "};
String [] ccs = {"you@abc.com "};
It. putExtra (Intent. EXTRA_EMAIL, tos );
It. putExtra (Intent. EXTRA_CC, ccs );
It. putExtra (Intent. EXTRA_TEXT, "The email body text ");
It. putExtra (Intent. EXTRA_SUBJECT, "The email subject text ");
It. setType ("message/rfc822 ");
StartActivity (Intent. createChooser (it, "Choose Email Client "));
Intent it = new Intent (Intent. A
listener class private OnClickListener itemsOnClick = new OnClickListener () {@ Overridepublic void onClick (View v) {menuWindow for the pop-up window. dismiss (); switch (v. getId () {// photograph case R. id. takePhotoBtn: Intent takeIntent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); // The following sentence specifies the path takeIntent for storing the photo after the camera is taken. putExtra (MediaStor
I.getauthority ());
/** * @param uri the URI to check.
* @return Whether The Uri authority is mediaprovider. */public static Boolean ismediadocument (Uri uri) {return ' com.android.providers.media.documents '. Equals (Uri.getaut
Hority ()); }/** * Get the value of the ' data column ' for this Uri.
This is useful to * Mediastore Uris, and other file-based contentproviders.
* * @param context.
* @param uri the URI to query. * @param
Obtain image resources from the SD card or take a new image.
Post Code first
Get image:
Note: You can specify the address to save the image when taking the photo.
[Java]CharSequence [] items = {"album", "camera "};New AlertDialog. Builder (this). SetTitle ("select image source "). SetItems (items, new OnClickListener (){Public void onClick (DialogInterface dialog, int which ){If (which = SELECT_PICTURE ){Intent intent = new Intent (Intent. ACTION_GET_CONTENT );Intent. addCategory (Intent. CATEGO
Sometimes we need to select an image as the Avatar or upload it to the album. In this case, we can choose to start the camera or choose from the image library.
First, let's see how to start the camera and take the photo.
The following code starts the camera and returns the photo:
Public void capturepicture () {// start and save the photo to the temporary file intent = new intent (); intent. setaction (mediastore. action_image_capture); intent. putext
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.